Skip to content

Instantly share code, notes, and snippets.

@bofm
Last active June 10, 2016 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bofm/31519b1668ef2c7f29c98d0c0c8685c6 to your computer and use it in GitHub Desktop.
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.
#!/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