Linux's System Services

An Operating System service supplies different kinds of services to both the users and to the programs as well. It also provides application programs (that run within an Operating system) an environment to execute it freely. It provides users the services run various programs in a convenient manner.

The mechanisms and techniques for managing services often differ by operating system. Here are some example services for the most common distributions and operating systems.

Our example service is Python’s SimpleHTTPServer web server. The command to start the server is:

python -m SimpleHTTPServer 8000

sysvinit (Debian and Ubuntu)

sysvinit is a collection of System V-style init programs originally written by Miquel van Smoorenburg. They include init, which is run by the kernel as process 1, and is the parent of all other processes.

Install

1) Edit the script to your liking, rename it and copy (as root) to /etc/init.d/. Then make it executable:

chmod +x /etc/init.d/example

2) Enable the daemon:

update-rc.d example defaults

3) Start the service:

service example start

sysvinit Script


#!/bin/sh

### BEGIN INIT INFO
# Provides:          exampledaemon
# 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: Example
# Description:       Example start-stop-daemon - Debian
### END INIT INFO

NAME="example"
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
APPDIR="/"
APPBIN="/usr/bin/python"
APPARGS="-m SimpleHTTPServer 8000"
USER="example"
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
  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 $?
}

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

exit 0

sysvinit (CentOS)

Same system as the above example, but for CentOS. Init controls the startup, running, and shutdown of all other programs.

Install

1) Edit the script to your liking, rename it and copy (as root) to /etc/init.d/. Then make it executable:

chmod +x /etc/init.d/example

2) Enable the config in in runlevels 2, 3, 4, and 5:

chkconfig example on

3) Start the service:

service example start

sysvinit Script


#!/bin/sh
#
# example start stop daemon for CentOS (sysvinit)
#
# chkconfig: - 64 36
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 2 3 4 6
# Required-Start:
# description: example start stop daemon for CentOS
# processname: python
# pidfile: none
# lockfile: /var/lock/subsys/example

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

USER="example"
APPNAME="example"
APPBIN="/usr/bin/python"
APPARGS="-m SimpleHTTPServer 8000"
LOGFILE="/var/log/$APPNAME/error.log"
LOCKFILE="/var/lock/subsys/$APPNAME"

LOGPATH=$(dirname $LOGFILE)

start() {
        [ -x $prog ] || exit 5
        [ -d $LOGPATH ] || mkdir $LOGPATH
        [ -f $LOGFILE ] || touch $LOGFILE

        echo -n $"Starting $APPNAME: "
        daemon --user=$USER "$APPBIN $APPARGS >>$LOGFILE &"
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $LOCKFILE
        return $RETVAL
}

stop() {
        echo -n $"Stopping $APPNAME: "
        killproc $APPBIN
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
        return $RETVAL
}

restart() {
        stop
        start
}

rh_status() {
        status $prog
}

rh_status_q() {
        rh_status >/dev/null 2>&1
}

case "$1" in
        start)
                rh_status_q && exit 0
                $1
        ;;
        stop)
                rh_status_q || exit 0
                $1
        ;;
        restart)
                $1
        ;;
        status)
                rh_status
        ;;
        *)
                echo $"Usage: $0 {start|stop|status|restart}"
                exit 2
esac

upstart (Ubuntu)

Upstart is an event-based replacement for the traditional init daemon—the method written by Scott James Remnant, a former employee of Canonical. It's event-driven model allows it to respond to events asynchronously as they are generated.

Install

1) Edit the script to your liking, rename it and copy (as root) to /etc/init/. Then make it executable:

chmod +x /etc/init/example.conf

2) Start the service:

sudo start example

upstart Script


author "User"
description "start and stop example for Ubuntu (upstart)"
version "1.0"

start on started networking
stop on runlevel [!2345]

env APPUSER="example"
env APPDIR="/usr/bin"
env APPBIN="python"
env APPARGS="-m SimpleHTTPServer 8000"

respawn

script
  exec su - $APPUSER -c "$APPDIR/$APPBIN $APPARGS"
end script

systemd (Arch Linux)

systemd is a system and service manager that runs as PID 1 and starts the rest of the system. The init system is used to bootstrap user space and manage user processes and provides fundamental building blocks for the operating system.

Install

1) Edit the script to your liking, rename it and copy (as root) to /etc/systemd/system/. Then make it executable:

chmod +x /etc/systemd/system/example.service

2) Enable the script on boot:

systemctl enable example

3) Start the service:

systemctl start example

systemd Script


[Unit]
Description=example

[Service]
ExecStart=/usr/bin/python -m http.server 8000
Type=simple
User=example
Group=example

[Install]
WantedBy=multi-user.target
{{ message }}

{{ 'Comments are closed.' | trans }}