Skip to content

Instantly share code, notes, and snippets.

@hirose31
Last active December 17, 2015 20:48
Show Gist options
  • Save hirose31/5669933 to your computer and use it in GitHub Desktop.
Save hirose31/5669933 to your computer and use it in GitHub Desktop.
run file for no-restarting daemon process
#!/bin/sh
#
# run file for no-restarting daemon process
# http://d.hatena.ne.jp/hirose31/20130529/1369831078
#
exec 2>&1
runfile=$(readlink -f $0)
rundir=${runfile%/*}
daemon=/home/hirose31/hoged
opts=''
pidfile="$rundir/pid"
if [ -e $pidfile ]; then
old_pid=$(cat $pidfile)
# remain daemon process when run process receive SIGKILL
# so try to kill old daemon process but THIS IS VERY DENGEROUS!!
# because not daemon process may be killed...
echo "$old_pid still exists"
case $old_pid in
[1-9][0-9]*)
echo "kill $old_pid"
kill -KILL $old_pid
;;
esac
echo "do svc -d me"
svc -d $rundir
exit
fi
pid=
# kill background daemon process if run process receive a signal
trap 'kill -TERM $pid' TERM
trap 'kill -TERM $pid' HUP
trap 'kill -TERM $pid' INT
echo "run in background: $daemon $opts"
setuidgid hirose31 \
env - PATH="/usr/local/bin:$PATH" \
envdir ./env \
$daemon $opts &
pid=$!
echo $pid > $pidfile
echo "wait for exiting $pid"
wait $pid
echo "$pid exited"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment