A very simple example of Linux service script

#!/bin/sh

### BEGIN INIT INFO
# Provides:          mySitedaemon
# Required-Start:    $local_fs $network $syslog
# Required-Stop:     $local_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mySite
# Description:       mySite start-stop-daemon - Debian
### END INIT INFO

NAME="mySite"
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
APPDIR="/home/server/"
APPBIN="/home/server/start.sh"
APPARGS=""
USER="root"
GROUP="example"

# Include functions 
set -e
. /lib/lsb/init-functions

start() {
  printf "Starting '$NAME'... "
#  start-stop-daemon --start --chuid "$USER:$GROUP" --background --make-pidfile --pidfile /var/run/$NAME.pid --chdir "$APPDIR" --exec "$APPBIN" -- $APPARGS || true
  echo start-stop-daemon --start --chuid "$USER" --background --make-pidfile --pidfile /var/run/$NAME.pid --chdir "$APPDIR" --exec "$APPBIN" -- $APPARGS || true
  start-stop-daemon --start --chuid "$USER" --background --make-pidfile --pidfile /var/run/$NAME.pid --chdir "$APPDIR" --exec "$APPBIN" -- $APPARGS || true
  # su $USER -c \"$SCMMANAGER_HOME/bin/scm-server start\" >> $SCMMANAGER_HOME/logs/startupSCM.log\r\n        sleep 2
  printf "done\n"
}

#We need this function to ensure the whole process tree will be killed
killtree() {
    local _pid=$1
    local _sig=${2-TERM}
    for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
        killtree ${_child} ${_sig}
    done
    kill -${_sig} ${_pid}
}

stop() {
  printf "Stopping '$NAME'... "
  [ -z `cat /var/run/$NAME.pid 2>/dev/null` ] || \
  while test -d /proc/$(cat /var/run/$NAME.pid); do
    killtree $(cat /var/run/$NAME.pid) 15
    sleep 0.5
  done 
  [ -z `cat /var/run/$NAME.pid 2>/dev/null` ] || rm /var/run/$NAME.pid
  printf "done\n"
}

status() {
  status_of_proc -p /var/run/$NAME.pid "" $NAME && exit 0 || exit $?
}

echo "Hello, world!" >&2

case "$1" in
  start)
	echo "Trying to start!" >&2
    start
    ;;
  stop)
	echo "Trying to stop!" >&2
    stop
    ;;
  restart)
	echo "Trying to restart!" >&2
    stop
    start
    ;;
  status)
	echo "Show me status!" >&2
    status
    ;;
  *)
    echo "Usage: $NAME {start|stop|restart|status}" >&2
    exit 1
    ;;
esac

exit 0
  1. Copy this script to /etc/init.d/example .
  2. Adjust the variables.
  3. Make sure the script is executable:
  4. chmod +x /etc/init.d/example
    Enable the daemon with:
  5. update-rc.d example defaults
    Start the service with:
    service example start