Swarm systemd service

From Wiki
Jump to navigationJump to search

Overview

After installing the files list below, enable the services to run at next boot with:

sudo systemctl enable swarmsrv
sudo systemctl enable swarmchat

/lib/systemd/system/swarmsrv.service

[Unit]
Description=Swarm communications server
After=rc-local.service
ConditionPathExists=!/home/swarm/swarmsrv/server.norun

[Service]
Type=simple
User=swarm
Group=swarm
RootDirectory=/
WorkingDirectory=/home/swarm/swarmsrv
EnvironmentFile=-/home/swarm/swarmsrv/server.opts
EnvironmentFile=-/etc/rpimodel
ExecStartPre=/bin/bash /home/swarm/swarmsrv/server_setup.sh
ExecStart=/home/swarm/swarmsrv/server --nostdout $SERVER_OPTS

[Install]
WantedBy=multi-user.target

/etc/init.d/swarmsrv

#!/bin/sh
# Start/stop swarmsrv
#
### BEGIN INIT INFO
# Provides:          swarmsrv
# Required-Start:    $syslog $network +ntp
# Required-Stop:     $null
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Swarm communications server daemon
# Description:       Swarm communications server daemon
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

. /lib/lsb/init-functions

SWARMSRV_DIR=/home/swarm/swarmsrv
SWARMSRV_PRE=${SWARMSRV_DIR}/server_setup.sh
SWARMSRV_EXE=${SWARMSRV_DIR}/server
SWARMSRV_LOG=/dev/null
SWARMSRV_PID=/var/run/swmarsrv.pid

start() {

   status_of_proc swarmsrv >/dev/null
   RETVAL=$?
   if [ "$RETVAL" = "0" ] ; then
       echo "swarmsrv is already started!"
   else
       if [ -r ${SWARMSRV_DIR}/${SWARMSRV_EXE}.opts ] ; then
         . ${SWARMSRV_DIR}/${SWARMSRV_EXE}.opts
       fi
       if [ -x ${SWARMSRV_PRE} ] ; then
         ${SWARMSRV_PRE}
       fi
       log_daemon_msg "Starting Swarm server" "swarmsrv"
       start-stop-daemon --exec ${SWARMSRV_EXE} --nostdout ${SERVER_OPTS} >${SWARMSRV_LOG} 2>&1 &
       log_end_msg $?
   fi

}

stop1() {
   start-stop-daemon --quiet --stop --oknodo --exec ${SWARMSRV_EXE} --retry 5 --stop
}

case "$1" in
start)
    start
    ;;
stop)
    status_of_proc swarmsrv > /dev/null
    RETVAL=$?
    if [ "$RETVAL" != "0" ] ; then
        echo "Swarm server not started!"
    else
        log_daemon_msg "Stopping Swarm server" "swarmsrv"
        stop1
        log_end_msg $?
    fi
    ;;
restart)
    $0 stop && sleep 2 && $0 start
    ;;

status)
   status_of_proc swarmsrv "swarmsrv"
   ;;

*)  log_action_msg "Usage: /etc/init.d/swarmsrv {start|stop|restart|status}"
    exit 2
    ;;
esac
exit 0

/lib/systemd/system/swarmchat.service

[Unit]
Description=Swarm text message server
After=swarmsrv.service
ConditionPathExists=!/home/swarm/chat/chat.norun

[Service]
Type=simple
User=swarm
Group=swarm
RootDirectory=/
WorkingDirectory=/home/swarm/chat
ExecStart=/usr/local/bin/node app.js

[Install]
WantedBy=multi-user.target

/etc/init.d/swarmchat

#!/bin/sh
# Start/stop swarmchat
#
### BEGIN INIT INFO
# Provides:          swarmchat
# Required-Start:    $syslog $network +ntp
# Required-Stop:     $null
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Swarm communications server daemon
# Description:       Swarm communications server daemon
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

. /lib/lsb/init-functions

SWARMCHAT_EXE=/home/swarm/chat/node app.js
SWARMCHAT_LOG=/dev/null
SWARMCHAT_PID=/var/run/swmarchat.pid

start() {

   status_of_proc swarmchat >/dev/null
   RETVAL=$?
   if [ "$RETVAL" = "0" ] ; then
      echo "swarmchat is already started!"
   else
       log_daemon_msg "Starting Swarm chat server" "swarmchat"
       start-stop-daemon --exec ${SWARMCHAT_EXE}> $SWARMCHAT_LOG 2>&1 &
       log_end_msg $?
   fi

}

stop1() {
   start-stop-daemon --quiet --stop --oknodo --exec ${SWARMCHAT_EXE} --retry 5 --stop
}

case "$1" in
start)
    start
    ;;
stop)
    status_of_proc swarmchat > /dev/null
    RETVAL=$?
    if [ "$RETVAL" != "0" ] ; then
        echo "Swarm server not started!"
    else
        log_daemon_msg "Stopping Swarm chat server" "swarmchat"
        stop1
        log_end_msg $?
    fi
    ;;
restart)
    $0 stop && sleep 2 && $0 start
    ;;

status)
   status_of_proc swarmchat "swarmchat"
   ;;

*)  log_action_msg "Usage: /etc/init.d/swarmchat {start|stop|restart|status}"
    exit 2
    ;;
esac
exit 0