Skip to content

Instantly share code, notes, and snippets.

@AgentOak
Created Sep 11, 2019
Embed
What would you like to do?
SysVinit service skeleton using start-stop-daemon
#!/bin/sh
#
# This init script is compatible with the LSB 4.1.0 Core Section 20.2.
# Modify the options above the vertical line only.
#
# This script was designed to be compatible with any daemon. Even if your
# program is not a daemon, you can specify "--background --make-pidfile" in
# START_OPTIONS to make it a daemon.
#
# Check out start-stop-daemon(8) for more options. All options can also be set
# in /etc/default/$NAME, which will be loaded automatically if it exists.
# If your daemon doesn't support reloading, leave RELOAD_SIGNAL empty and
# "force-reload" will default to "restart". If your daemon reloads its
# configuration files automatically, set RELOAD_SIGNAL to "auto".
#
# If PIDDIR is set, this script will create the pidfile directory and change its
# owner to the user of the process, so that a daemon can write its pidfile even
# after privileges drop. If the daemon doesn't delete its own pidfile on exit,
# the script will remove it.
#
# Copyright (c) 2014-2016 Jan Erik Petersen
# This file is distributed unter the GNU General Public License v3.0.
#
### BEGIN INIT INFO
# Provides: myserver
# Required-Start: $remote_fs $local_fs $network
# Required-Stop: $remote_fs $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: MyServer Platform
# Description: MyServer is a highly scalable buzzword buzzword platform.
### END INIT INFO
PATH=/sbin:/usr/local/sbin:/usr/sbin:/bin:/usr/local/bin:/usr/bin
DESC="MyServer Platform"
NAME=myserver
# For demonstration purposes, the variables are preconfigured to run a php
# script as a daemon, which has no daemon capabilities itself.
USER=www-data
PIDDIR=/run/$NAME
PIDFILE=$PIDDIR/$NAME.pid
DAEMON=`which php`
DAEMON_ARGS="server.php"
MATCH_OPTIONS="--pidfile $PIDFILE --exec $DAEMON --user $USER"
START_OPTIONS="--background --make-pidfile --chdir /opt/myserver --chuid $USER"
STOP_RETRY="TERM/30/KILL/10"
RELOAD_SIGNAL=""
###############################################################################
if [ `id -u` -ne 0 ]; then
log_failure_msg "The $NAME init script must be run as root"
exit 4
fi
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
[ -x "$DAEMON" ] || exit 5
. /lib/init/vars.sh
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
[ -n "$PIDDIR" ] && mkdir -p $PIDDIR && chown $USER: $PIDDIR
start-stop-daemon --quiet --oknodo $MATCH_OPTIONS --start $START_OPTIONS -- $DAEMON_ARGS
case "$?" in
0) log_end_msg 0; exit 0 ;;
*) log_end_msg 1; exit 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
start-stop-daemon --quiet --oknodo $MATCH_OPTIONS --stop --retry $STOP_RETRY
case "$?" in
0) log_end_msg 0; rm -f $PIDFILE; exit 0 ;;
*) log_end_msg 1; exit 1 ;;
esac
;;
status)
start-stop-daemon --quiet $MATCH_OPTIONS --status
case "$?" in
0) log_action_msg "$DESC is running with PID `cat $PIDFILE`"; exit 0 ;;
1) log_warning_msg "$DESC is not running, but $PIDFILE exists. The daemon probably died"; exit 1 ;;
3) log_action_msg "$DESC is not running"; exit 3 ;;
*) log_failure_msg "Unable to determine status of $DESC"; exit 4 ;;
esac
;;
reload)
if [ -z "$RELOAD_SIGNAL" ]; then
log_failure_msg "Reloading $NAME is not supported"
exit 3
fi
log_daemon_msg "Reloading $DESC" "$NAME"
if [ "$RELOAD_SIGNAL" = "auto" ]; then
log_end_msg 0
exit 0
fi
start-stop-daemon --quiet --oknodo $MATCH_OPTIONS --stop --signal $RELOAD_SIGNAL
case "$?" in
0) log_end_msg 0; exit 0 ;;
*) log_end_msg 1; exit 1 ;;
esac
;;
force-reload)
[ -n "$RELOAD_SIGNAL" ] && $0 reload || $0 try-restart
exit $?
;;
restart)
start-stop-daemon --quiet $MATCH_OPTIONS --status
if [ $? -eq 0 ]; then
$0 stop || exit 1
fi
$0 start
exit $?
;;
try-restart)
start-stop-daemon --quiet $MATCH_OPTIONS --status
[ $? -ne 0 ] && exit 0
$0 stop || exit 1
$0 start
exit $?
;;
*)
echo "Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}" >&2
exit 2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment