Skip to content

Instantly share code, notes, and snippets.

@allaryin
Created April 24, 2017 17:08
Show Gist options
  • Save allaryin/755a912e7ad36f159875e31a8e8c1511 to your computer and use it in GitHub Desktop.
Save allaryin/755a912e7ad36f159875e31a8e8c1511 to your computer and use it in GitHub Desktop.
Run a daemonized process in the "foreground", sort of
#!/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