Created
April 24, 2017 17:08
-
-
Save allaryin/755a912e7ad36f159875e31a8e8c1511 to your computer and use it in GitHub Desktop.
Run a daemonized process in the "foreground", sort of
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
#!/bin/sh | |
# | |
# I found the original version of this on stackexchange but forget where. | |
# | |
set -eu | |
pidfile="$1" | |
cmdline="$2" | |
# Trap and proxy the signals :) | |
function kill_app() { | |
kill $(cat $pidfile) | |
exit 0 | |
} | |
trap "kill_app" SIGINT SIGTERM | |
# Launch the daemon | |
${cmdline} | |
sleep 2 | |
# Wait for the daemon to exit | |
while [ -f ${pidfile} ] && kill -0 $(cat $pidfile) ; do | |
sleep 0.5 | |
done | |
# unexpected exit | |
exit 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment