You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
0 commit comments