move exit trap to function

main
Rachel Lambda Samuelsson 2023-08-08 20:58:17 +02:00
parent 2c41800782
commit 719c03887d
1 changed files with 6 additions and 1 deletions

View File

@ -4,6 +4,11 @@ log() {
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"
[ -d ./matabas-db ] || {
log "Non-existent, creating postgres directory (./matabas-db)"
@ -18,7 +23,7 @@ log "Checking for existing postgres directory"
log "Starting postgres"
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"
createdb -p 2137 matabas 2>>./matabas-db/matabas.log || log "Database already exists, not overwriting"