Skip to content

Instantly share code, notes, and snippets.

@Purexo
Last active November 14, 2015 12:09
Show Gist options
  • Save Purexo/6ae67d133799fe581b08 to your computer and use it in GitHub Desktop.
Save Purexo/6ae67d133799fe581b08 to your computer and use it in GitHub Desktop.
#!/bin/bash
#################################
# Author : Purexo <contact@purexo.eu>
# usage :
# ./script {start|stop|force-stop|restart|reload|force-reload|status}
# number c'est pour le loadbalancing on va avoir plusieurs fois
# le même programme de lancé, faut pouvoir fermer ce que l'on veux
#################################
root=$(readlink -f $(dirname $0))
cd $root
PATH='/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games'
SCRIPTNAME="$0"
DESC='Description du Daemon'
DAEMONNAME="NomDuProcessus"
DAEMON="/usr/bin/node" # programme à lancer
DAEMONUSER='user' # Utilisateur, éviter root
DAEMONARGS="--- Argument à donner au binaire ---" # pour superpowers, le chemin de lanceur de serveur de Superpowers
PIDFILE="$root/pid/$DAEMONNAME.pid"
USAGE() {
echo "Usage: $root/$SCRIPTNAME {start|stop|force-stop|restart|reload|force-reload|status} {server}"
echo "server : probably test or prod"
}
if [ $# -lt 2 ]; then
USAGE
exit 1
fi
mkdir -p "$root/pid/"
. /lib/lsb/init-functions
GETPID () {
cat "$PIDFILE"
}
do_start () {
log_daemon_msg "Starting system $DAEMONNAME Daemon"
start-stop-daemon --name $DAEMONNAME --chuid "$DAEMONUSER" \
--background --start --exec "$DAEMON" \
--pidfile "$PIDFILE" --make-pidfile -- "$DAEMONARGS"
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMONNAME Daemon"
start-stop-daemon --pidfile "$PIDFILE" \
--stop --retry 10
log_end_msg $?
}
case "$1" in
start)
do_start
;;
stop)
do_stop
rm $PIDFILE
;;
restart|reload|force-reload)
do_stop
do_start
;;
force-stop)
do_stop
killall -q GETPID || true
sleep 2
killall -q -9 GETPID || true
rm $PIDFILE
;;
status)
status_of_proc -p $PIDFILE $DAEMON $DAEMONNAME && exit 0 || exit $?
;;
*)
USAGE
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment