12.3 Shutting Down Properly

It is very important that you shut down your system properly. Simply turning the power off with the power switch can cause serious filesystem damage. While the system is on, files are in use even if you aren't doing anything. Remember that there are many processes running in the background all the time. These processes are managing the system and keep a lot of files open. When the system's power is switched off, these files are not closed properly and may become corrupted. Depending on what files become damaged, the system might be rendered completely unusable! In any case, you'll have to go through a long filesystem check procedure on the next reboot.

Note

If you configured your system with a journalling filesystem, like ext3 or reiserfs, you'll be partially protected from filesystem damage, and your filesystem check on reboot will be shorter than if you had used a filesystem without journalling, like ext2. However, this safety net is no excuse for improperly shutting down your system! A journalling FS is meant to protect your files from events beyond your control, not from your own laziness.

In any case, when you want to reboot or power down your computer, it is important to do so properly. There are several ways of doing so; you can pick whichever one you think is the most fun (or least amount of work). Since a shutdown and a reboot are similar procedures, most of the ways for powering off the system can also be applied to rebooting.

The first method is through the shutdown(8) program, and it is probably the most popular. shutdown can be used to reboot or turn off the system at a given time, and can display a message to all the logged-in users of the system telling them that the system is going down.

The most basic use of shutdown to power down the computer is:

# shutdown -h now

In this case, we are not going to send a custom message to the users; they will see shutdown's default message. “now” is the time that we want to shutdown, and the “-h” means to halt the system. This is not a very friendly way to run a multi-user system, but it works just fine on your home computer. A better method on a multiuser system would be to give everyone a little advance warning:

# shutdown -h +60

This would shutdown the system in one hour (60 minutes), which would be just fine on a normal multiuser system. Vital systems should have their downtime scheduled far in advance, and you should post warnings about the downtime in any appropriate locations used for system notifications (email, bulletin board, /etc/motd, whatever).

Rebooting the system uses the same command, but substitutes “-r” for “-h”:

# shutdown -r now

You can use same time notation with shutdown -r that you could with shutdown -h. There are a lot of other things that you can do with shutdown to control when to halt or reboot the machine; see the man page for more details.

The second way of shutting down or powering off the computer is to use the halt(8) and reboot(8) commands. As the names indicate, halt will immediately halt the operating system, and reboot will reboot the system. (reboot is actually just a symbolic link to halt.) They are invoked like so:

# halt
# reboot

A lower-level way to reboot or shutdown the system is to talk directly to init. All the other methods are simply convenient ways to talk to init, but you can directly tell it what to do using telinit(8) (note that it only has one “l”). Using telinit will tell init what runlevel to drop into, which will cause a special script to be run. This script will kill or spawn processes as needed for that runlevel. This works for rebooting and shutting down because both of those are special runlevels.

# telinit 0

Runlevel 0 is halt mode. Telling init to enter runlevel 0 will cause all processes to be killed off, the filesystems unmounted, and the machine to be halted. This is a perfectly acceptable way to bring down the system. On many laptops and modern desktop computers, this will also cause the machine to be turned off.

# telinit 6

Runlevel 6 is reboot mode. All processes will be killed off, the filesystems will be unmounted, and the machine will be rebooted. This is a perfectly acceptable method of rebooting the system.

For the curious, when switching to runlevel 0 or 6, whether by using shutdown, halt, or reboot, the script /etc/rc.d/rc.6 is run. (The script /etc/rc.d/rc.0 is another symbolic link, to /etc/rc.d/rc.6.) You can customize this file to your tastes--but be sure to test your changes carefully!

There is one last method of rebooting the system. All the other methods require you to be logged in as root. However, it is possible to reboot the machine even if you aren't root, provided that you have physical access to the keyboard. Using Control+Alt+Delete (the "three-fingered salute") will cause the machine to immediately reboot. (Behind the scenes, the shutdown command is called for you when you use Control+Alt+Delete.) The salute doesn't always work when using X Windows--you may need to use Control+Alt+F1 (or another Function key) to switch to a non-X Windows terminal before using it.

Finally, the file that ultimately controls every aspect of startup and shutdown is the /etc/inittab(5) file. In general, you should not need to modify this file, but it may give you insight into why some things work the way they do. As always, see the man pages for further details.