Last active
June 10, 2016 22:29
-
-
Save bofm/31519b1668ef2c7f29c98d0c0c8685c6 to your computer and use it in GitHub Desktop.
Tiny init script in plain shell. Suitable for Docker. Handles signals and forwards them to the child processes for a clean shutdown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
# Define your services here. One line per service. | |
services=' | |
php-fpm -F | |
nginx -g "daemon off;" | |
' | |
pids="" | |
die(){ [ -n "$pids" ] && kill $pids 2>/dev/null; wait $pids; } | |
trap die SIGINT SIGTERM | |
# Starting services | |
while IFS= read -r s ; do | |
if [ -n "$s" ]; then | |
eval "$s &" || die | |
pids="$pids ""$!" | |
fi | |
done << EE | |
$services | |
EE | |
# Healthchecker. Kills all services if any service goes down. Starts in a subshell. | |
( | |
while true; do | |
kill -0 $pids 2> /dev/null || { die && exit;} | |
sleep 2 | |
done | |
) & | |
wait $pids |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment