Skip to content

Instantly share code, notes, and snippets.

@vkoloss
Forked from alobato/start-stop-example.sh
Last active December 31, 2017 11:23
Show Gist options
  • Save vkoloss/b0d8859723cb91694371e98e2a8046e6 to your computer and use it in GitHub Desktop.
Save vkoloss/b0d8859723cb91694371e98e2a8046e6 to your computer and use it in GitHub Desktop.
start-stop-daemon-example
#!/bin/bash
# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e
NAME=app
DIR=/home/username/app/bin
PIDFILE=/var/run/$NAME.pid
DAEMON=/home/username/app/bin/executable
DAEMON_ARGS="-foo bar"
USER=username
LOG=/var/log/$NAME.log
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
case "$1" in
start)
echo -n "Starting daemon: "$NAME
start-stop-daemon --start --quiet --chdir $DIR --chuid $USER --make-pidfile --pidfile $PIDFILE \
--background --startas /bin/bash -- -c "exec $DAEMON $DAEMON_ARGS > $LOG 2>&1"
echo "."
;;
stop)
echo -n "Stopping daemon: "$NAME
start-stop-daemon --stop --quiet --chuid $USER --oknodo --remove-pidfile --pidfile $PIDFILE
echo "."
;;
restart)
echo -n "Restarting daemon: "$NAME
start-stop-daemon --stop --quiet --chuid $USER --oknodo --retry 30 --remove-pidfile --pidfile $PIDFILE
start-stop-daemon --start --quiet --chdir $DIR --chuid $USER --make-pidfile --pidfile $PIDFILE \
--background --startas /bin/bash -- -c "exec $DAEMON $DAEMON_ARGS > $LOG 2>&1"
echo "."
;;
*)
echo "Usage: "$1" {start|stop|restart}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment