Table Of Contents

History

ubuntu: sysVinit –> upstart(6.10+) –> systemd (15.04+)

Features of Upstart

Event Based

  • Start System faster (compare with previous method).
  • Dynamically start service when discovering new device
  • Dynamically stop service when device is removed

Systemd

Systemd is a collection of system management daemons, utilities and libraries which serves as a replacement of System V init daemon. Systemd functions as central management and configuration platform for UNIX like system.

systemctl

Systemctl is a systemd utility which is responsible for Controlling the systemd system and service manager.

Init script vs Service file

You may use service SCRIPT start|stop|statu.. manage jobs, but it’s different between using Init script and upstart or systemd service file. see man service.

System V init script

located in /etc/init.d/SCRIPT

  • Example: (from shadowsocks-go project)
#!/bin/bash
# Start/stop shadowsocks.
#
### BEGIN INIT INFO
# Provides:          shadowsocks
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: shadowsocks is a lightweight tunneling proxy
# Description:       Modified from Linode's nginx fastcgi startup script
### END INIT INFO

# Note: this script requires sudo in order to run shadowsocks as the specified
# user. 

BIN=/usr/bin/shadowsocks-local
CONFIG_FILE=/etc/shadowsocks/config.json
LOG_FILE=/var/log/shadowsocks
USER=nobody
GROUP=nobody
PID_DIR=/var/run
PID_FILE=$PID_DIR/shadowsocks.pid
RET_VAL=0

\[ -x $BIN \] || exit 0

check_running() {
  if [\[ -r $PID_FILE \]]; then
    read PID <$PID_FILE
    if [\[ -d "/proc/$PID" \]]; then
      return 0
    else
      rm -f $PID_FILE
      return 1
    fi
  else
    return 2
  fi
}

do_status() {
  check_running
  case $? in
    0)
      echo "shadowsocks running with PID $PID"
      ;;
    1)
      echo "shadowsocks not running, remove PID file $PID_FILE"
      ;;
    2)
      echo "Could not find PID file $PID_FILE, shadowsocks does not appear to be running"
      ;;
  esac
  return 0
}

do_start() {
  if [\[ ! -d $PID_DIR \]]; then
    echo "creating PID dir"
    mkdir $PID_DIR || echo "failed creating PID directory $PID_DIR"; exit 1
    chown $USER:$GROUP $PID_DIR || echo "failed creating PID directory $PID_DIR"; exit 1
    chmod 0770 $PID_DIR
  fi
  if check_running; then
    echo "shadowsocks already running with PID $PID"
    return 0
  fi
  if [\[ ! -r $CONFIG_FILE \]]; then
    echo "config file $CONFIG_FILE not found"
    return 1
  fi
  echo "starting shadowsocks"
  # sudo will set the group to the primary group of $USER
  sudo -u $USER $BIN -c $CONFIG_FILE >>$LOG_FILE &
  PID=$!
  echo $PID > $PID_FILE
  sleep 0.3
  if ! check_running; then
    echo "start failed"
    return 1
  fi
  echo "shadowsocks running with PID $PID"
  return 0
}

do_stop() {
  if check_running; then
    echo "stopping shadowsocks with PID $PID"
    kill $PID
    rm -f $PID_FILE
  else
    echo "Could not find PID file $PID_FILE"
  fi
}

do_restart() {
  do_stop
  do_start
}

case "$1" in
  start|stop|restart|status)
    do_$1
    ;;
  *)
    echo "Usage: shadowsocks {start|stop|restart|status}"
    RET_VAL=1
    ;;
esac

exit $RET_VAL

You can use update-rc.d command to update the system service definitions.

Systemd Unit file

Example (/etc/systemd/system/shadowsocks.service)

  1. Prepare the executable file with the custom service.
  2. Create a unit file in the /etc/systemd/system/ directory and make sure it has correct file permissions. Execute as root:
touch /etc/systemd/system/shadowsocks.service
chmod 644 /etc/systemd/system/shadowsocks.service
  1. Edit shadowsocks.service file
  • Shadowsocks-local
[Unit]
Description=Shadowsocks local daemon
After=network.target

[Service]
ExecStart=/usr/bin/shadowsocks-local -c /etc/shadowsocks/config.json
PIDFile=/var/run/shadowsocks.pid
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=default.target
  • Shadowsocks-server
[Unit]                                                                          
Description=Go Shadowsocks server daemon                                          
Wants=network-online.target
After=network.target network-online.target multi-user.target
                                                                                 
[Service]                                                                        
ExecStart=/usr/bin/shadowsocks-server -u -c /etc/shadowsocks/config.json  
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/var/run/shadowsocks.pid                                                 
KillMode=process                                                                 
Restart=on-failure                                                               
TimeoutStopSec=30
                                                                                 
[Install]                                                                        
WantedBy=multi-user.target 
Alias=ss-server.service
  • Proxy setting
Environment="HTTP_PROXY=http://127.0.0.1:8118"
Environment="HTTPS_PROXY=http://127.0.0.1:8118"
Environment="NO_PROXY=pi.lan,127.0.0.1"

Check Environment setting [example home-assistant-@pi.servie]:

sudo systemctl show home-assistant@pi.service --property Environment
# Output:
# Environment=HTTP_PROXY=http://127.0.0.1:8118 HTTPS_PROXY=http://127.0.0.1:8118 NO_PROXY=pi.lan,127.0.0.1
  1. Notify systemd that a new shadowsocks.service file exists by executing the following command as root
systemctl daemon-reload
# start shadowsocks service
systemctl start shadowsocks.service

You can check its status now by service shadowsocks status To add it to startup, using systemctl enable shadowsocks

  • Check details of running status
service shadowsocks status -l
# or
journalctl -xe

upstart job is configured in /etc/init/

  1. Shell script with systemd unit

A sample with java web application.

dummy service

[Unit]                                                                          
Description=Dummy daemon                                          
Wants=network-online.target
After=network.target network-online.target multi-user.target
                                                                                 
[Service]                                                                        
ExecStart=/usr/bin/dummy 
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/var/run/dummy.pid                                                 
KillMode=process                                                                 
Restart=on-failure                                                               
TimeoutStopSec=30
                                                                                 
[Install]                                                                        
WantedBy=multi-user.target 

dummy script to start

#/bin/bash

JAVA_HOME="${JAVA_HOME:-/usr/lib/jvm/default}"
JAVA_OPTS="-XX:+UnlockExperimentalVMOptions -XX:-UseJVMCICompiler"
JAVA_OPTS="${JAVA_OPTS} -Xms1024m -Xmx2048m -XX:PermSize=32m"
DUMMY_PROGRAM="/opt/dummy/dummy.jar"
START_CMD="${JAVA_HOME}/bin/java ${JAVA_OPTS} -jar ${DUMMY_PROGRAM}"

LOG_FILE=/var/log/dummy

init() {
	touch ${LOG_FILE}
}

check_running() {
	logger -i -t "It's running" -f ${LOG_FILE}
}

check_deps() {
	echo "Ok"
}

start() {
	eval ${START_CMD}
	echo "It's started"		
}

main() {
 init
 check_running
 check_deps
 start &
}

main "$@"

Reference