Skip to content

Instantly share code, notes, and snippets.

@cdparra
Created October 17, 2015 02:05
Show Gist options
  • Save cdparra/16d79c0cb5775156236c to your computer and use it in GitHub Desktop.
Save cdparra/16d79c0cb5775156236c to your computer and use it in GitHub Desktop.
#!/bin/sh
### BEGIN INIT INFO
# Provides: appname
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts appname server
# Description: starts appname server using start-stop-daemon
### END INIT INFO
### HOW TO
# 1. cd $APP_DIR
# 2. activator stage
# 3. ln -s $APP_DIR /usr/share/appname
# 4. cp $THIS_FILE /etc/init.d
# 5. chmod +x /etc/init.d/$THIS_FILE
# 6. update-rc.d appcivist-backend defaults
# 7. service appname start
###
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/node/bin"
APP_DIR="/usr/share/appname"
APP_BIN="target/universal/stage/bin/appname"
USER="appname"
GROUP="appname"
DESC="appname description"
NAME="appname"
set -e
. /lib/lsb/init-functions
start() {
echo "Starting $DESC... "
start-stop-daemon --start --chuid "$USER:$GROUP" --background --exec $APP_DIR/$APP_BIN
echo "done"
}
#We need this function to ensure the whole process tree will be killed
killtree() {
local _pid=$1
local _sig=${2-TERM}
for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
killtree ${_child} ${_sig}
done
kill -${_sig} ${_pid}
}
stop() {
echo "Stopping $DESC... "
if test -f /var/run/$NAME/play.pid; then
while test -d /proc/$(cat /var/run/$NAME/play.pid); do
killtree $(cat /var/run/$NAME/play.pid) 15
sleep 0.5
done
rm /var/run/$NAME/play.pid
fi
echo "done"
}
status() {
status_of_proc -p /var/run/$NAME/play.pid "" "appname" && exit 0 || exit $?
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
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