move exit trap to function

This commit is contained in:
Rachel Lambda Samuelsson 2023-08-08 20:58:17 +02:00
parent 2c41800782
commit 719c03887d

View File

@ -4,6 +4,11 @@ log() {
printf '\e[1m\e[38;5;87m==>\e[0m\e[1m %s.\e[0m\n' "$*" printf '\e[1m\e[38;5;87m==>\e[0m\e[1m %s.\e[0m\n' "$*"
} }
exitHook() {
log "Stopping postgres"
pg_ctl stop --pgdata=./matabas-db
}
log "Checking for existing postgres directory" log "Checking for existing postgres directory"
[ -d ./matabas-db ] || { [ -d ./matabas-db ] || {
log "Non-existent, creating postgres directory (./matabas-db)" log "Non-existent, creating postgres directory (./matabas-db)"
@ -18,7 +23,7 @@ log "Checking for existing postgres directory"
log "Starting postgres" log "Starting postgres"
pg_ctl start --pgdata=./matabas-db --log=./matabas-db/matabas.log --options="-p 2137 -k /tmp" pg_ctl start --pgdata=./matabas-db --log=./matabas-db/matabas.log --options="-p 2137 -k /tmp"
trap 'log "Stopping postgres" && pg_ctl stop --pgdata=./matabas-db' EXIT trap exitHook EXIT
log "Creating database" log "Creating database"
createdb -p 2137 matabas 2>>./matabas-db/matabas.log || log "Database already exists, not overwriting" createdb -p 2137 matabas 2>>./matabas-db/matabas.log || log "Database already exists, not overwriting"