Skip to content

Commit 43dde6e

Browse files
Make container exit gracefully and with correct exit code
While fiddling around with multiple child processes, and giving them some nice "exit 1" error codes, I noticed that the container would always exit with exit code "0". It was not until I placed an "exit 2" above the 'trap "kill 0" EXIT' row that I started getting the expected exit codes again. I dived a bit deeper into this, and realized that this thing with properly handling kill signals is pretty tedious. Nevertheless, after reading a lot of articles (linked at the bottom) I finally managed to create something which seems to work as intended. The first trap will catch the SIGINT (Ctrl+C) and the SIGTERM (docker stop ...) singnals, and make so that both of these trigger a normal "exit" command in the script. The second trap is special for bash (the 'EXIT' at the end of the trap command), since this allows you to trigger a "cleanup script" before exiting the main program. I made so that it will run the function "clean_exit" before terminating with the correct exit code. The "clean_exit" script just sends a SIGTERM signal to the PIDs of our two child processes (if they exists and have started), which gives them a chance to exit gacefully. By implementing this we can now exit the container in a controlled and graceful manner, while also having it report the correct exit code depending on what happened. [1]: http://veithen.io/2014/11/16/sigterm-propagation.html [2]: https://www.linuxjournal.com/content/bash-trap-command [3]: https://blog.codeship.com/trapping-signals-in-docker-containers/ [4]: https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/ [5]: https://unix.stackexchange.com/a/444676 [6]: https://blog.phusion.nl/2015/01/20/docker-and-the-pid-1-zombie-reaping-problem/ [7]: https://stackoverflow.com/a/35410993 [8]: https://unix.stackexchange.com/q/317492
1 parent b7074f6 commit 43dde6e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/scripts/entrypoint.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
#!/bin/bash
22

3-
# When we get killed, kill all our children (o.O)
4-
trap "exit" INT TERM
5-
trap "kill 0" EXIT
3+
# Helper function to gracefully shut down our child processes when we exit.
4+
clean_exit() {
5+
for PID in $NGINX_PID $CERTBOT_LOOP_PID; do
6+
if kill -0 $PID 2>/dev/null; then
7+
kill -SIGTERM "$PID"
8+
wait "$PID"
9+
fi
10+
done
11+
}
12+
13+
# Make bash listen to the SIGTERM and SIGINT kill signals, and make them trigger
14+
# a normal "exit" command in this script. Then we tell bash to execute the
15+
# "clean_exit" function, seen above, in the case an "exit" command is triggered.
16+
# This is done to give the child processes a chance to exit gracefully.
17+
trap "exit" TERM INT
18+
trap "clean_exit" EXIT
619

720
# Source "util.sh" so we can have our nice tools.
821
. $(cd $(dirname $0); pwd)/util.sh

0 commit comments

Comments
 (0)