1.106.2

Change runlevels and shutdown or reboot system

Weight 3

Linux Professional Institute Certification — 102

                Angus Lees gus@inodes.org

Geoffrey Robertson ge@ffrey.com

               Nick Urbanik nicku@nicku.org

2005 July

Description of Objective

Candidates should be able to manage the runlevel of the system. This objective includes changing to single user mode, shutdown or rebooting the system. Candidates should be able to alert users before switching runlevel, and properly terminate processes. This objective also includes setting the default runlevel.

Key files, terms, and utilities include:

/etc/inittab
shutdown
init

Change runlevels and shutdown or reboot system

Candidates should be able to manage the runlevel of the system. This objective includes changing to single user mode, shutdown or rebooting the system. Candidates should be able to alert users before switching runlevel, and properly terminate processes. This objective also includes setting the default runlevel.

\underline{2}

Boot the system

/etc/inittab
shutdown
init
/sbin/init

“init” is the process started by the kernel after booting. It is up to init to start the rest of the system. The usual “SysV init” does this according to /etc/inittab.

The kernel also treats process ID 1 (init) specially:

• PID 1 is skipped by various “kill all” operations, etc

• “orphaned” child processes are adopted by PID 1

Telling init what to do

By sending signals directly (as root) or by running various commands, which relay to

init:

telinit Tell init to reload inittab, re-exec itself or switch runlevels

shutdown Initiate a shutdown by doing some book-keeping and then signalling init

halt, reboot, poweroff Tools used to initiate or finalise special kinds of shutdown

runlevel Find out current or previous runlevel

Runlevels

   The Linux Standards Base (http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/runlevels.html)

defines the following standard runlevels that all distributions should follow to be compliant:

0 halt
1 single user mode
2 multiuser with no network services exported
3 normal/full multiuser
4 reserved for local use, default is normal/full multiuser
5 multiuser with a display manager or equivalent
6 reboot

Passed through via kernel command line, telinit or default in /etc/inittab.

/etc/inittab

id : runlevels : action : process

# /etc/inittab: init(8) configuration.
id:5:initdefault:
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
# /etc/init.d executes the S and K scripts
# scripts when change runlevel.
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

/etc/init.d/rc runs all the “K” scripts in /etc/rcN.d/, followed by the “S” scripts.

/etc/inittab
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
# When our UPS tells us power has
# failed, assume we have a few minutes
# of power left.  Schedule a shutdown
# for 2 minutes from now.
# This does, of course, assume you have
# powerd installed and your
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
/etc/inittab
# Run gettys in standard runlevels
#co:2345:respawn:/sbin/agetty ttyS0 38400 vt100
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
# Run xdm in runlevel 5

x:5:once:/etc/X11/prefdm -nodaemon
#co:2345:respawn:/sbin/agetty ttyS0 38400 vt100
S1:2345:respawn:/sbin/mgetty ttyS1
/etc/inittab
# Example how to put a getty on a serial line (for a terminal)
#
#T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100
#T1:23:respawn:/sbin/getty -L ttyS1 9600 vt100
# Example how to put a getty on a modem line.
#
#T3:23:respawn:/sbin/mgetty -x0 -s 57600 ttyS3

Initiating a shutdown

   shutdown [options] time [message]

Popular options:

-r Reboot

-h Halt

-c Cancel a running shutdown

“time” can be HH:MM (eg 17:30) or +minutes (eg +5) or “now”

Examples:

shutdown -r now
shutdown -h 17:30 Scheduled hardware maintenance

License Of This Document

Copyright \begin{matrix}c \\ ◯ \end{matrix} 2005, 2003 Angus Lees <gus@inodes.org>, Geoffrey Robertson <ge@ffrey.com> and Nick Urbanik <nicku@nicku.org>. Permission is granted to make and distribute verbatim copies or modified versions of this document provided that this copyright notice and this permission notice are preserved on all copies under the terms of the GNU General Public License as published by the Free Software Foundation—either version 2 of the License or (at your option) any later version.