placing shell script under systemd control

Posted by Calvin Cheng on Server Fault See other posts from Server Fault or by Calvin Cheng
Published on 2012-05-09T08:11:59Z Indexed on 2012/09/22 15:39 UTC
Read the original article Hit count: 357

Filed under:
|
|
|

Assuming I have a shell script like this:-

#!/bin/sh
# cherrypy_server.sh

PROCESSES=10
THREADS=1 # threads per process
BASE_PORT=3035 # the first port used
# you need to make the PIDFILE dir and insure it has the right permissions
PIDFILE="/var/run/cherrypy/myproject.pid"
WORKDIR=`dirname "$0"`
cd "$WORKDIR"

cp_start_proc()
{
 N=$1
 P=$(( $BASE_PORT + $N - 1 ))
 ./manage.py runcpserver daemonize=1 port=$P pidfile="$PIDFILE-$N" threads=$THREADS request_queue_size=0 verbose=0
}

cp_start()
{
 for N in `seq 1 $PROCESSES`; do
  cp_start_proc $N
 done
}

cp_stop_proc()
{
 N=$1
 #[ -f "$PIDFILE-$N" ] && kill `cat "$PIDFILE-$N"`
 [ -f "$PIDFILE-$N" ] && ./manage.py runcpserver pidfile="$PIDFILE-$N" stop
 rm -f "$PIDFILE-$N"
}

cp_stop()
{
 for N in `seq 1 $PROCESSES`; do
  cp_stop_proc $N
 done
}

cp_restart_proc()
{
 N=$1
 cp_stop_proc $N
 #sleep 1
 cp_start_proc $N
}

cp_restart()
{
 for N in `seq 1 $PROCESSES`; do
  cp_restart_proc $N
 done
}

case "$1" in
 "start")
  cp_start
 ;;
 "stop")
  cp_stop
 ;;
 "restart")
  cp_restart
 ;;
 *)
  "$@"
 ;;
esac

From the bash script, we can essentially do 3 things:

  1. start the cherrypy server by calling ./cherrypy_server.sh start
  2. stop the cherrypy server by calling ./cherrypy_server.sh stop
  3. restart the cherrypy server by calling ./cherrypy_server.sh restart

How would I place this shell script under systemd's control as a cherrypy.service file (with the obvious goal of having systemd start up the cherrypy server when a machine has been rebooted)?

Reference systemd service file example here - https://wiki.archlinux.org/index.php/Systemd#Using_service_file

© Server Fault or respective owner

Related posts about bash

Related posts about init.d