Keeping Syncthing Running: Systemd & regular /etc/init.d

This is my version of init script for debian. As you can see it runs only one instance and reduces the priority of the process. Also need to add the update block, but that’s for later.

(Git)

#!/bin/sh
### BEGIN INIT INFO
# Provides: syncthing
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Syncthing Daemon
# Description: Syncthing replaces proprietary sync and cloud services with something open, 
#              trustworthy and decentralized. 
#              Your data is your data alone and you deserve to choose where it is stored, 
#              if it is shared with some third party and how it's transmitted over the Internet.
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Syncthing"
NAME=syncthing
# DIR should be the path to the folder where the binary resides
DIR=/usr/bin
DAEMON=$DIR/$NAME
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Arguments that should be forwarded to the binary (e.g.: "-home=/etc/syncthing")
DAEMON_ARGS=""

# Permissions
USER=www-data
GROUP=www-data


[ -x "$DAEMON" ] || exit 0

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
    if [ -e $PIDFILE ]; then
        PID=`cat $PIDFILE`

        if ( ps -p $PID > /dev/null ); then
            log_failure_msg "$DESC '$NAME' is already running."
            return 1
        else
            rm -f $PIDFILE

            start-stop-daemon --start --background --chdir $DIR --chuid $USER:$GROUP --make-pidfile --pidfile $PIDFILE --quiet --iosched idle --nicelevel 19 --exec $DAEMON --test > /dev/null || return 1
            start-stop-daemon --start --background --chdir $DIR --chuid $USER:$GROUP --make-pidfile --pidfile $PIDFILE --quiet --iosched idle --nicelevel 19 --exec $DAEMON -- $DAEMON_ARGS || return 2
        fi
    else
        start-stop-daemon --start --background --chdir $DIR --chuid $USER:$GROUP --make-pidfile --pidfile $PIDFILE --quiet --iosched idle --nicelevel 19 --exec $DAEMON --test > /dev/null || return 1
        start-stop-daemon --start --background --chdir $DIR --chuid $USER:$GROUP --make-pidfile --pidfile $PIDFILE --quiet --iosched idle --nicelevel 19 --exec $DAEMON -- $DAEMON_ARGS || return 2
    fi
}

do_stop() {
    if [ -e $PIDFILE ]; then
        PID=`cat $PIDFILE`

        if ( ps -p $PID > /dev/null ); then
            start-stop-daemon --stop --retry 1 --signal 2 --quiet --pidfile $PIDFILE
            [ "$?" = 2 ] && return 2
        else
            log_failure_msg "$DESC '$NAME' is not running."
            rm -f $PIDFILE
            return 1
        fi
    else
        log_failure_msg "$DESC '$NAME' is not running."
        return 1
    fi
}

case "$1" in
    start)
        log_daemon_msg "Starting $DESC..." "$NAME"
        do_start

        case "$?" in
            0|1) log_end_msg 0 ;;
            1) log_end_msg 1 ;;
        esac
    ;;
    stop)
        log_daemon_msg "Stopping $DESC..." "$NAME"
        do_stop

        case "$?" in
            0|1) log_end_msg 0 ;;
            2) log_end_msg 1 ;;
        esac
    ;;
    restart)
        log_daemon_msg "Restarting $DESC..." "$NAME"
        do_stop

        case "$?" in
            0|1)
                do_start

                case "$?" in
                    0) log_end_msg 0 ;;
                    *) log_end_msg 1 ;;
                esac
            ;;
            *)
                log_end_msg 1
            ;;
        esac
    ;;
    status)
        if [ -e $PIDFILE ]; then
            PID=`cat $PIDFILE`

            if ( ps -p $PID > /dev/null ); then
                log_success_msg "$DESC '$NAME' is running (pid $PID)."
                exit 0
            else
                log_failure_msg "$DESC '$NAME' is not running."
                rm -f $PIDFILE
                exit 1
            fi
        else
            log_failure_msg "$DESC '$NAME' is not running."
            exit 1
        fi
    ;;
    *)
        log_action_msg "Usage: $SCRIPTNAME {start|stop|restart|status}"
        exit 0
    ;;
esac