These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/Lowlevel System Control

Classnotes | UNIX01 | RecentChanges | Preferences

We have seen before (see /Inittab And Boot Scripts) that the Linux system has a series of "runlevels" which it switches into at different stages during its boot/shutdown and normal operation cycles.

We have also seen that at a very high-level, Linux can be switched between runlevels by clicking various "Shutdown" or "Reboot" buttons at the graphical user interface. But how do we control these run-levels if we are only operating from the command-line?

telinit

Recall that init is the master of all processes. Your system is essentially "spawned" from init. Also recall that we briefly mentionned the command telinit as a means for switching between runlevels.

telinit is actually just a special link to the init program. It takes a character, and then calls init, instructing it to perform some function based upon that character.

The telinit command has the following usage (as specified from it's man page):

 telinit [ -t sec ] [ 0123456sSQqabcUu ]

Where:

0,1,2,3,4,5 or 6
tell init to switch to the specified run level.
a,b,c
tell init to process only those /etc/inittab file entries having runlevel a,b or c.
Q or q
tell init to re-examine the /etc/inittab file.
S or s
tell init to switch to single user mode.
U or u
tell init to re-execute itself (preserving the state). No re-examining of /etc/inittab file happens. Run level should be one of Ss12345, otherwise request would be silently ignored.

When init switches runlevels, it must first stop all of the processes which will not be active in the next runlevel (recall that some runlevels do not have networking, naturally all networking subsystems should be stopped... additionally, if switching to runlevel 6, we are rebooting, so all processes must be stopped). init does this by first sending the SIGTERM signal. After a certain duration, if the process has not yet quit, init will send a SIGKILL signal to it.

You can adjust the duration of time init waits for processes to terminate with the "-t sec" option. You specify the number of seconds to wait between the SIGTERM and SIGKILL signals. This number usually defaults to 5 seconds on most UNIX systems.

While you can technically use the telinit command to shutdown or reboot a Linux machine, thats not what it usually used for. The most common uses for the telinit command are to switch to single user mode (e.g., during some sort of network attack) or cause init to reread inittab without rebooting.

In case you haven't already figured it out, Linux (and most UNIXes) very rarely require reboots. As we have seen, you can drastically redefine the system's init parameters, and cause the system to reload its inittab file to enact those changes without ever having to take the system offline. Just about the only time you really have to reboot is when you update your kernel.

shutdown

The classic UNIX command for bringing a system down (or rebooting) is the shutdown command. Most GUI tools for shutting the system down will actually be calling shutdown behind the scenes.

Shutdown takes several parameters at the command line, and then usually instructs init to switch runlevels. Shutdown is a better option for reboots/shutdowns than telinit because it does it's job in a more secure way: It sends warning messages to all logged in users, blocks all incoming logins, and sends warning signals to all processes (thus giving them a chance to exit cleanly). Because of it's benefits over just plain telinit, you should really only think of telinit as a "last-resort" for bringing your system down.

Shutdown has the following usage:

 /sbin/shutdown [-t sec] [-arkhncfF] time [warning-message]

Where:

-a
Use /etc/shutdown.allow. If the file exists, it compares the login names in that file with the list of people that are logged in in a virtual console. Only if one of those authorized users or root is logged in, it will proceed.
-t sec
Tell init to wait sec seconds between sending processes the warning and the kill signal, before changing to another runlevel.
-k
Don't really shutdown; only send the warning messages to everybody.
-r
Reboot after shutdown.
-h
Halt after shutdown.
-n
DEPRECATED! Don't call init to do the shutdown but do it ourself. The use of this option is discouraged, and its results are not always what you'd expect.
-f
Skip fsck on reboot.
-F
Force fsck on reboot.
-c
Cancel an already running shutdown. With this option it is of course not possible to give the time argument, but you can enter a explanatory message on the command line that will be sent to all users.
time
When to shutdown (we'll look at the format shortly).
warning-message
Message to send to all users.

The time argument can have different formats. First, it can be an absolute time in the format hh:mm, in which hh is the hour (1 or 2 digits) and mm is the minute of the hour (in two digits). Second, it can be in the format +m, in which m is the number of minutes to wait. The word now is an alias for +0.

So, for example, what would the following shutdown commands do?

 1) shutdown -h now
 2) shutdown -rf +30 "Sorry folks, gotta reboot"
 3) shutdown -k now "INTRUDER ALERT"
 4) shutdown -c "Whoops, I'm no 1337 4aXor..."
 5) shutdown -rF 00:00 "Daily FS check."

halt, reboot

On many UNIX systems, there exist additional programs which, in turn, supply and even more simplified interface to shutdown. These programs are known generally as halt and reboot.

Often, these two map almost literally to:

   halt == shutdown -h now
 reboot == shutdown -r now

but do not assume this will always be the case regardless of UNIX.

halt and reboot take the following options:

 halt [-n] [-w] [-d] [-f] [-i] [-p]
 reboot [-n] [-w] [-d] [-f] [-i]

Where:

-n
Don't sync before reboot or halt.
-w
Don't actually reboot or halt but only write the wtmp record (in the /var/log/wtmp file).
-d
Don't write the wtmp record. The -n flag implies -d.
-f
Force halt or reboot, don't call shutdown (<i>on some systems, this is akin to telinit'ing into the associated runlevel).
-i
Shut down all network interfaces just before halt or reboot.
-p
When halting the system, do a poweroff. This is the default when halt is called as poweroff. This may be useful on equipment which has buggy BIOSes that do not translate ACPI requests correctly.


Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited August 8, 2003 10:06 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.