From 4e1037a8876abf2a996fe15091e8e2b04f60bbfc Mon Sep 17 00:00:00 2001 From: Kris Anderson <1562427+kris-anderson@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:10:03 -0800 Subject: [PATCH] Docker: exit promptly on SIGTERM instead of waiting to be killed liberate.sh runs as PID 1, and bash as PID 1 ignores SIGTERM unless it is trapped, so `docker stop` always waited out its timeout and then SIGKILLed the container (exit 137). Trap TERM and INT. Run LibationCli and the sleep between runs in the background and wait on them, so the trap fires at once instead of after the child exits. On a signal, send SIGTERM to a running child and wait for it, so LibationCli can shut down cleanly before the script exits with 143 (TERM) or 130 (INT). Co-Authored-By: Claude Opus 5.5 --- Docker/liberate.sh | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/Docker/liberate.sh b/Docker/liberate.sh index 41246527..c4c7e7f1 100755 --- a/Docker/liberate.sh +++ b/Docker/liberate.sh @@ -157,16 +157,40 @@ setup_db() { ln -s "${FILE}" "${LINK}" } +# Run a command in the background and wait for it. bash only runs a trap once the +# foreground command exits, so `docker stop` would otherwise wait out a whole scan, +# liberate or sleep. +run_child() { + "$@" & + CHILD_PID=$! + wait "${CHILD_PID}" + local child_exit=$? + CHILD_PID= + return "${child_exit}" +} + +# Docker sends SIGTERM to PID 1 only, and bash as PID 1 ignores it unless it is trapped. +shutdown() { + info "received ${1}, shutting down" + if [ -n "${CHILD_PID}" ]; then + # Always forward TERM: background children ignore SIGINT + kill -TERM "${CHILD_PID}" 2>/dev/null + wait "${CHILD_PID}" + fi + info "exiting" + exit "${2}" +} + run() { info "scanning accounts" - /libation/LibationCli scan + run_child /libation/LibationCli scan local scan_exit=$? if [ "${scan_exit}" -ne 0 ]; then error "scan failed (exit ${scan_exit}); skipping liberate. If the log shows Failed to decrypt ExistingAccessToken, see https://getlibation.com/docs/frequently-asked-questions#docker-finds-no-new-books-failed-to-decrypt-existingaccesstoken" exit "${scan_exit}" fi info "liberating books" - /libation/LibationCli liberate + run_child /libation/LibationCli liberate } main() { @@ -214,6 +238,9 @@ main() { info "running every ${SLEEP_TIME}" fi + trap 'shutdown SIGTERM 143' TERM + trap 'shutdown SIGINT 130' INT + # loop while true do @@ -224,7 +251,7 @@ main() { break fi - sleep "${SLEEP_TIME}" + run_child sleep "${SLEEP_TIME}" done info "exiting"