Gaurav Mishra
<gmishx@gmail.com>
Linux - 4
/etc/rc.d 26-02-2018
Gaurav Mishra <gmishx@gmail.com>
Services in Linux
• Linux environment support services which are daemons running in background.
• These services can be make to start at boot.
• Linux distros have directories /etc/rc.d and /etc/init.d which hold scripts to run
these services.
• Every script in these directories is invoked at the boot time.
• When the system boots, it looks for scripts in /etc/rc.d directory followed by
/etc/init.d, run them and setup the system for use.
• When the system shutdowns, it first halts scripts in /etc/init.d then moves to
/etc/rc.d
26-02-2018
Gaurav Mishra <gmishx@gmail.com>
Run level in Unix like systems
• A runlevel is a preset operating state on a Unix-like operating system.
• A system can be booted into (i.e., started up into) any of several runlevels, each of which is
represented by a single digit integer.
• Each runlevel designates a different system configuration and allows access to a different
combination of processes (i.e., instances of executing programs).
• The are differences in the runlevels according to the operating system. Seven runlevels are
supported in the standard Linux kernel (i.e., core of the operating system). They are:
0 - System halt; no activity, the system can be safely powered down.
1 - Single user; rarely used.
2 - Multiple users, no NFS (network filesystem); also used rarely.
3 - Multiple users, command line (i.e., all-text mode) interface; the standard runlevel for
most Linux-based server hardware.
4 - User-definable
5 - Multiple users, GUI (graphical user interface); the standard runlevel for most Linux-
based desktop systems.
6 - Reboot; used when restarting the system.
26-02-2018
Gaurav Mishra <gmishx@gmail.com>
/etc/rc.d structure
Structure for Debian based distros
26-02-2018
Gaurav Mishra <gmishx@gmail.com>
Contents of directories
The scripts in this directory are executed each time the system enters
single-user mode.
Their purpose is to stop all services and thus to put the system in
single-user mode.
• The scripts in this directory are executed once when entering
runlevel 0 (shutdown).
• The scripts are all symbolic links whose targets are located in
/etc/init.d/.
• Their purpose is to stop all services and to make the system
ready for shutdown.
/etc/rc2.d/
• Run level 6 for reboot.
• The purpose of the scripts in this directory is to stop all services
and to make the system ready for reboot.
• Multi-user mode with GUI.
• Their purpose is to start all services in their GUI mode for
multi-user.
/etc/rc6.d/
/etc/rc0.d/ /etc/rc1.d/
Gaurav Mishra <gmishx@gmail.com>
Contents of directories
Contains scripts used by the System V init tools (SysVinit).
Files in /etc/init.d are shell scripts that respond to start, stop, restart
commands to manage a particular service.
These scripts can be invoked directly or (most commonly) via some
other trigger (typically the presence of a symbolic link in /etc/rc?.d/).
• The scripts in this directory are executed once when booting the
system, even when booting directly into single user mode.
/etc/init/
• Contains configuration files used by Upstart.
• Files in /etc/init are configuration files telling Upstart how and
when to start, stop, reload the configuration, or query the
status of a service.
/etc/rcS.d/ /etc/init.d/
Gaurav Mishra <gmishx@gmail.com>
Skeleton of service script
#! /bin/sh
### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should
be used to construct scripts to be
# placed in
/etc/init.d.
### END INIT INFO
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --
pidfile $PIDFILE --exec $DAEMON --
$DAEMON_ARGS || return 2
}
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
26-02-2018
Gaurav Mishra <gmishx@gmail.com>
Skeleton of service script
# other if a failure occurred
start-stop-daemon --stop --quiet --
retry=TERM/30/KILL/5 --pidfile
$PIDFILE --name $NAME
rm -f $PIDFILE
return "$?"
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
status)
status_of_proc "$DAEMON" "$NAME" &&
exit 0 || exit $?
;;
restart|force-reload)
do_stop
do_start
;;
*)
#echo "Usage: $SCRIPTNAME
{start|stop|restart|reload|force-
reload}" >&2
echo "Usage: $SCRIPTNAME
{start|stop|status|restart|force-
reload}" >&2
exit 3
;;
esac
26-02-2018
Gaurav Mishra <gmishx@gmail.com>
Auto start script
1. Create the service script in /etc/init.d/ with a
unique name.
2. Insert links
1. update-rc.d syntax:
update-rc.d [-n] name start|stop NN runlevel
[runlevel]... . start|stop NN runlevel
[runlevel]... . ...
update-rc.d [-n] name defaults|disable|enable [
S|2|3|4|5 ]
2. Using the defaults:
update-rc.d foobar defaults
3. Equivalent command using explicit
argument sets:
update-rc.d foobar start 20 2 3 4 5 . stop 20 0 1 6
.
4. Insert links at default runlevels when B
requires A
update-rc.d script_for_A defaults 80 20
update-rc.d script_for_B defaults 90 10
5. Remove all links for a script:
update-rc.d foobar remove
6. Example of a command for installing a
system initialization-and-shutdown
script:
update-rc.d foobar start 45 S . stop 31 0 6 .
Example links created using defaults:
Adding system startup for /etc/init.d/foobar
...
/etc/rc0.d/K20foobar -> ../init.d/foobar
/etc/rc1.d/K20foobar -> ../init.d/foobar
/etc/rc6.d/K20foobar -> ../init.d/foobar
/etc/rc2.d/S20foobar -> ../init.d/foobar
/etc/rc3.d/S20foobar -> ../init.d/foobar
/etc/rc4.d/S20foobar -> ../init.d/foobar
/etc/rc5.d/S20foobar -> ../init.d/foobar
26-02-2018

Linux Run Level

  • 1.
  • 2.
    Gaurav Mishra <gmishx@gmail.com> Servicesin Linux • Linux environment support services which are daemons running in background. • These services can be make to start at boot. • Linux distros have directories /etc/rc.d and /etc/init.d which hold scripts to run these services. • Every script in these directories is invoked at the boot time. • When the system boots, it looks for scripts in /etc/rc.d directory followed by /etc/init.d, run them and setup the system for use. • When the system shutdowns, it first halts scripts in /etc/init.d then moves to /etc/rc.d 26-02-2018
  • 3.
    Gaurav Mishra <gmishx@gmail.com> Runlevel in Unix like systems • A runlevel is a preset operating state on a Unix-like operating system. • A system can be booted into (i.e., started up into) any of several runlevels, each of which is represented by a single digit integer. • Each runlevel designates a different system configuration and allows access to a different combination of processes (i.e., instances of executing programs). • The are differences in the runlevels according to the operating system. Seven runlevels are supported in the standard Linux kernel (i.e., core of the operating system). They are: 0 - System halt; no activity, the system can be safely powered down. 1 - Single user; rarely used. 2 - Multiple users, no NFS (network filesystem); also used rarely. 3 - Multiple users, command line (i.e., all-text mode) interface; the standard runlevel for most Linux-based server hardware. 4 - User-definable 5 - Multiple users, GUI (graphical user interface); the standard runlevel for most Linux- based desktop systems. 6 - Reboot; used when restarting the system. 26-02-2018
  • 4.
    Gaurav Mishra <gmishx@gmail.com> /etc/rc.dstructure Structure for Debian based distros 26-02-2018
  • 5.
    Gaurav Mishra <gmishx@gmail.com> Contentsof directories The scripts in this directory are executed each time the system enters single-user mode. Their purpose is to stop all services and thus to put the system in single-user mode. • The scripts in this directory are executed once when entering runlevel 0 (shutdown). • The scripts are all symbolic links whose targets are located in /etc/init.d/. • Their purpose is to stop all services and to make the system ready for shutdown. /etc/rc2.d/ • Run level 6 for reboot. • The purpose of the scripts in this directory is to stop all services and to make the system ready for reboot. • Multi-user mode with GUI. • Their purpose is to start all services in their GUI mode for multi-user. /etc/rc6.d/ /etc/rc0.d/ /etc/rc1.d/
  • 6.
    Gaurav Mishra <gmishx@gmail.com> Contentsof directories Contains scripts used by the System V init tools (SysVinit). Files in /etc/init.d are shell scripts that respond to start, stop, restart commands to manage a particular service. These scripts can be invoked directly or (most commonly) via some other trigger (typically the presence of a symbolic link in /etc/rc?.d/). • The scripts in this directory are executed once when booting the system, even when booting directly into single user mode. /etc/init/ • Contains configuration files used by Upstart. • Files in /etc/init are configuration files telling Upstart how and when to start, stop, reload the configuration, or query the status of a service. /etc/rcS.d/ /etc/init.d/
  • 7.
    Gaurav Mishra <gmishx@gmail.com> Skeletonof service script #! /bin/sh ### BEGIN INIT INFO # Provides: skeleton # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Example initscript # Description: This file should be used to construct scripts to be # placed in /etc/init.d. ### END INIT INFO do_start() { # Return # 0 if daemon has been started # 1 if daemon was already running # 2 if daemon could not be started start-stop-daemon --start --quiet -- pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS || return 2 } do_stop() { # Return # 0 if daemon has been stopped # 1 if daemon was already stopped # 2 if daemon could not be stopped 26-02-2018
  • 8.
    Gaurav Mishra <gmishx@gmail.com> Skeletonof service script # other if a failure occurred start-stop-daemon --stop --quiet -- retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME rm -f $PIDFILE return "$?" } case "$1" in start) do_start ;; stop) do_stop ;; status) status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? ;; restart|force-reload) do_stop do_start ;; *) #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force- reload}" >&2 echo "Usage: $SCRIPTNAME {start|stop|status|restart|force- reload}" >&2 exit 3 ;; esac 26-02-2018
  • 9.
    Gaurav Mishra <gmishx@gmail.com> Autostart script 1. Create the service script in /etc/init.d/ with a unique name. 2. Insert links 1. update-rc.d syntax: update-rc.d [-n] name start|stop NN runlevel [runlevel]... . start|stop NN runlevel [runlevel]... . ... update-rc.d [-n] name defaults|disable|enable [ S|2|3|4|5 ] 2. Using the defaults: update-rc.d foobar defaults 3. Equivalent command using explicit argument sets: update-rc.d foobar start 20 2 3 4 5 . stop 20 0 1 6 . 4. Insert links at default runlevels when B requires A update-rc.d script_for_A defaults 80 20 update-rc.d script_for_B defaults 90 10 5. Remove all links for a script: update-rc.d foobar remove 6. Example of a command for installing a system initialization-and-shutdown script: update-rc.d foobar start 45 S . stop 31 0 6 . Example links created using defaults: Adding system startup for /etc/init.d/foobar ... /etc/rc0.d/K20foobar -> ../init.d/foobar /etc/rc1.d/K20foobar -> ../init.d/foobar /etc/rc6.d/K20foobar -> ../init.d/foobar /etc/rc2.d/S20foobar -> ../init.d/foobar /etc/rc3.d/S20foobar -> ../init.d/foobar /etc/rc4.d/S20foobar -> ../init.d/foobar /etc/rc5.d/S20foobar -> ../init.d/foobar 26-02-2018