System V style init and systemd in practice

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Starting Scripts (init)

The init service invokes the start and stop scripts belonging to the applicable runlevel so that the desired system state can be established. In Debian based systems, such as Ubuntu, you will find the scripts under /etc/init.d .

If you use the package manager of your Linux distribution, you will find that the scripts have already been set up with the installation of the service. It is seldom the case that you will need to do a manual set up. However, if you do find yourself needing a script, then you should use the files that already exist as a model [3].

Listing 2 shows the start and stop scripts for atd , which are set up by the distribution.

Listing 2

Start and Stop Scripts for atd

01 #! /bin/sh
02 ### BEGIN INIT INFO
03 # Provides:          atd
04 # Required-Start:    $syslog $time $remote_fs
05 # Required-Stop:     $syslog $time $remote_fs
06 # Default-Start:     2 3 4 5
07 # Default-Stop:      0 1 6
08 # Short-Description: Deferred execution scheduler
09 # Description:       Debian init script for the atd
                        deferred executions
10 #                    scheduler
11 ### END INIT INFO
12 #
13 # Author:       Ryan Murray <rmurray@debian.org>
14 #
15
16 PATH=/bin:/usr/bin:/sbin:/usr/sbin
17 DAEMON=/usr/sbin/atd
18 PIDFILE=/var/run/atd.pid
19
20 test -x $DAEMON || exit 0
21
22 . /lib/lsb/init-functions
23
24 case "$1" in
25   start)
26         log_daemon_msg "Starting deferred execution
           scheduler" "atd"
27         start_daemon -p $PIDFILE $DAEMON
28         log_end_msg $?
29     ;;
30   stop)
31         log_daemon_msg "Stopping deferred
           execution scheduler" "atd"
32         killproc -p $PIDFILE $DAEMON
33         log_end_msg $?
34     ;;
35   force-reload|restart)
36     $0 stop
37     $0 start
38     ;;
39   status)
40     status_of_proc -p $PIDFILE $DAEMON atd && exit 0 || exit $?
41     ;;
42   *)
43     echo "Usage: /etc/init.d/atd {start|stop|restart|force-reload|status}"
44     exit 1
45     ;;
46 esac
47
48 exit 0

When you modify the configuration of a service (i.e., a daemon), you can manually stop and restart the service with this script (Figure 1). You can also ask about the daemon's. To execute the desired action, you should include start , stop , and status when you call the script.

Figure 1: Stop, start, and status inquiry of a service using an init script.

Runlevels and Directories (init)

Each runlevel has its own directory where there are links that point to /etc/init.d/[SCRIPTNAME] . The link name begins with an S when it stands for a start script or with K for kill script .

The alphabetical order determines the processing sequence. When a service is installed with a package manager, its links receive appropriate name. When you compile a service, then you need to figure out the order yourself. For example, you must remember a web server can only be started once the network is already running. In the worst case, you can always name your script S99zzzzz .

The runlevel directories rc0.d , rc1.d , rc2.d , rc3.d , rc4.d , rc5.d , rc6.d , and rcS.d are located under /etc . In the latest Debian and Ubuntu versions, the directories rc3.d , rc4.d , and rc5.d have almost identical content to rc2.d and are not usually used. This is an opportunity for the tinkerer; you can, for example, create groupings of server services.

The script for this example atd is located in /etc/init.d . The S15atd link for startup is found in /etc/rc2.d . The links that init calls to end this daemon are found in several directories, namely /etc/rc0.d , /etc/rc1.d , and /etc/rc6.d . In each, the link is named K01atd . This action is presented more clearly in Listing 3.

Listing 3

Starting Services Using atd

Within the <I>/etc<I> directory:
init.d/atd
rc0.d/K01atd -> ../init.d/atd
rc1.d/K01atd -> ../init.d/atd
rc2.d/S15atd -> ../init.d/atd
rc3.d/S15atd -> ../init.d/atd
rc4.d/S15atd -> ../init.d/atd
rc5.d/S15atd -> ../init.d/atd
rc6.d/K01atd -> ../init.d/atd

Buy this article as PDF

Express-Checkout as PDF

Pages: 1

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Systemd as central control for the Linux system

    Some Linux followers see systemd as the best thing since sliced bread – for others, it's the work of the devil. However, it also has the stuff to clear out old trenches and form a unified base for Linux.

  • Innovation in the Linux environment

    Testers constantly encounter new trends in the experimental branches of Linux distributions. I look at who drives Linux forward and how the future is shaping up.

  • What is Upstart?

    Originally created for use in Ubuntu, Upstart is suitable for deployment in all Linux distributions as an alternative to the System-V init.

  • Installing and testing Nextcloud

    Leading ownCloud developers, including the project founder Frank Karlitschek, became dissatisfied with the direction of the project, so they started Nextcloud, a fork of the code and a new company. The goal is to create a better balance among the company, clients, and users. We take a look at how Nextcloud is faring.

  • Use multiple distributions at the same time with Bedrock Linux

    Bedrock transparently combines virtually any number of distributions with different architectures, package management, and init systems under a filesystem.