How to create a SysVInit script (prior to systemd)

On systems based on SysVinit, init is the first process that is executed once the Linux kernel loads. Daemons are managed by shell scripts in /etc/init.d/. They call /etc/init.d/functions to start/stop the daemon process, and control its status via pid file. Optionally extra options are in /etc/sysconfig/ as env variables and configuration is placed in /etc/.

service@man runs (starts/stops/..) a System V init script, chkconfig@man is used to update and query on what runlevel services start.

$ cat /usr/bin/hello.sh
#!/bin/bash
while true; do logger "hello world"; sleep 10; done
$ chmod +x /usr/bin/hello.sh

## init script
$ cp /usr/share/doc/initscripts-*/sysvinitfiles /etc/init.d/hello
$ chmod +x /etc/init.d/hello
$ cat /etc/init.d/hello
#!/bin/sh
#
# <daemonname> <summary>
#
# chkconfig:   <default runlevel(s)> <start> <stop>
# description: <description, split multiple lines with a backslash>

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

exec="/usr/bin/hello.sh"
prog="hello"
config=""

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    #[ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    # if not running, start it up here, usually something like "daemon $exec"
    daemon $exec &
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    # stop it here, often "killproc $prog"
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic 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
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

## service start/stop/status
$ service hello start
Starting hello:
$ service hello status
hello (pid 17429) is running...
$ service hello stop

from SysVInitScript@fedora
see also SysVinit@arch

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s