Skip to content

Instantly share code, notes, and snippets.

@mytskine
Created April 15, 2013 17:07
Show Gist options
  • Save mytskine/5389623 to your computer and use it in GitHub Desktop.
Save mytskine/5389623 to your computer and use it in GitHub Desktop.
Wrapper script for running a Yesod app (xmlittre) as a daemon. start-stop-daemon starts the process, fork it in the background, and creates the PID file.
#! /bin/sh
DAEMON=xmlittre
NAME=xmlittre-web
DESC=xmlittre-web
MAINDIR=$(readlink -f `dirname "$0"`)
PIDPATH="$MAINDIR/tmp/run"
START_OPTS="--make-pidfile --background --no-close --chdir $MAINDIR"
DAEMON_OPTS="production --port 3000"
LOGPATH="$MAINDIR/tmp/log"
LOG="$LOGPATH/access.log"
LOG_ERR="$LOGPATH/error.log"
DAEMON="$MAINDIR/$DAEMON"
test -e $DAEMON || DAEMON="$MAINDIR/dist/build/xmlittre/xmlittre"
test -x $DAEMON || exit 0
test -e $LOGPATH || mkdir -p $LOGPATH || exit 1
test -e $PIDPATH || mkdir -p $PIDPATH || exit 1
set -e
. /lib/lsb/init-functions
daemon_start () {
start-stop-daemon --start --pidfile $PIDPATH/$NAME.pid \
--exec $DAEMON $START_OPTS -- $DAEMON_OPTS >> $LOG 2>> $LOG_ERR || true
}
daemon_stop () {
start-stop-daemon --stop --pidfile $PIDPATH/$NAME.pid \
--exec $DAEMON && rm "$PIDPATH/$NAME.pid"
}
case "$1" in
start)
echo -n "Starting $DESC: "
echo -n "Starting $DESC: " >> $LOG
daemon_start
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
echo -n "Stopping $DESC: " >> $LOG
daemon_stop
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
echo -n "Restarting $DESC: " >> $LOG
daemon_stop
sleep 1
daemon_start
echo "$NAME."
;;
status)
status_of_proc -p $PIDPATH/$NAME.pid "$DAEMON" xmlittre && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment