Monday, May 18, 2009

Stopping the System


For those of you who hadn't noticed, Linux isn't like DOS or Windows. Despite the superficial similarity at the command prompt and similarities in the GUI, they have little in common. One very important difference is the way you stop the system.

In DOS or Windows 95/98/ME, you are completely omnipotent. You know everything that's going on. You have complete control over everything. If you decide that you've had enough and flip the power switch, you are the only one doing so will effect. However, with dozens of people working on an Linux system and dozens more using its resources, simply turning off the machine is not something you want to do. Despite the fact that you will annoy quite a few people, it can cause damage to your system, depending on exactly what was happening when you killed the power. (Okay, you could also create problems with a DOS system, but with only one person, the chances are less likely).

On a multi-user system like Linux, many different things are going on. You many not see any disk activity, but the system may still have things its buffers which are waiting for the chance to write to the hard disk. If you turn off the power before this data is written, what is on the hard disk may be inconsistent.

Normally, pressing Ctrl-Alt-Del will reboot your system. You can prevent this by creating the file /etc/shutdown.allow, which contains a list (one entry per line) of users. If this file exists, the system will first check whether one of the users listed in shutdown.allow is logged in on the system console. If none are, you see the message

shutdown: no authorized users logged in.

To make sure that things are stopped safely, you need to shut down your system "properly." What is considered proper can be a couple of things, depending on the circumstances. Linux provides several tools to stop the system and allows you to decide what is proper for your particular circumstance. Flipping the power switch is not shutting down properly.

Note that the key combination Ctrl-Alt-Del is just a convention. There is nothing magic about that key combination, other than people are used to it from DOS/Windows. By default, the combination Ctrl-Alt-Del is assigned to the special keymap "Boot". This is typically defined by default in the file /usr/src/linux/drivers/char/defkeymap.map, which is the keyboard mapping the kernel uses when it boots. However, you can use the loadkeys program to change this if you need to.

If necessary, you could define that the combination Ctrl-Alt-Del is not assigned to anything, therefore it would not shutdown your system. However, should the system get stuck in a state that you cannot correct, shutting it down with Ctrl-Alt-Del is often the only safe alternative (as compared with simply flipping the power switch.)

When you press the "boot" key combination, the init program is sent the signal SIGINT. What init does will depend on how the /etc/inittab is configured. In the section on run levels, we talked about the various actions in /etc/inittab that tell init what to do when the key combination Ctrl-Alt-Del is pressed (one being ctraltdel). On my system it is defined as "/sbin/shutdown -r -t 4 now", which says to run the shutdown command immediately (now) and reboot (-r), waiting four seconds between the time the warning message is sent and the shutdown procedure is started (-t 4).

The first two tools to stop your system are actually two links in /sbin: halt and reboot, that link to the same file. If either of these is called and the system is not in run-level 0 or 6, then shutdown (also in /sbin) is called instead.

Running shutdown is really the safest way of bringing your system down, although you could get away with running init 0. This would bring the system down, but would not give the users any warning. Shutdown can be configured to give the users enough time to stop what they are working on and save all of their data.

Using the shutdown command, you have the ability not only to warn your users that the system is going down but also to give them the chance to finish up what they were doing. For example, if you were going to halt the system in 30 minutes to do maintenance, the command might look like this:

shutdown -h +30 "System going down for maintenance. Back up after lunch."

This message will appear on everyone's screen immediately, then at increasing intervals, until the system finally goes down.

If you have rebuilt your kernel or made other changes that require you to reboot your system, you can use shutdown as well, by using the -r option.

Option Description
-d Do not write to the /var/log/wtmp.
-f Force a reboot, i.e. do not call shutdown.
-i Shutdown the network interfaces before halting or rebooting.
-n Do not sync (write data to disk) before halting or rebooting.
-p Power off after shutdown.
-w Do not actually stop the system, just write to /var/log/wmtp.

Table - Options to halt and reboot.

Option Description
-c Cancel a shutdown that is in progress.
-f Don't run fsck when the system reboots (i.e. a "fast" reboot).
-F Force fsck on reboot.
-h Halt the system when the shutdown is completed.
-k Send a warning message, but do not actually shutdown the system.
-n Shutdown without calling init. DEPRECATED.
-r Reboot the system after shutdown.
-t seconds Seconds to wait before starting the shutdown.
-z Shutdown using "software suspend".

Table - Options to shutdown.

No comments:

Post a Comment