May 9, 2005

CLI Magic: shutdown

Author: Joe Barr

Many new Linux users feel uncomfortable about not having a fast-acting, easy-on, easy-off method of ending a runaway computer session, like the one they had with Windows. Something like the three-finger salute which is often used as a panacea for all sorts of ills under Windows. They need the comfort and reassurance that a quick, easy, and graceful exit -- but something short of using the BRS (Big Red Switch) -- can provide. Linux provides a quick and graceful shutdown, too. At least it does from the command line. It's called shutdown. Let's visit the CLI and learn a little more about how to make a graceful exit.Using shutdown is easy. You only have to learn two important things before you get your blackbelt in shutdown. To learn the first lesson, enter the following at the command line:


shutdown

That's right, it won't run if you try to run it as a normal user. On my SUSE system, I get an error message which says "command not found" if I'm not in superuser mode. That's because shutdown lives in the /sbin directory, which is not included in my path. But even if I specify the full path name and enter /sbin/shutdown, it still won't run. In that case, SUSE scolds me and says "shutdown: you must be root to do that!" The reason root privileges are required is because shutdown impacts all the users on the system. You're just logging out of your session, you are shutting down your machine and everyone else's sessions as well.

So let's try it again, grasshopper. This time as root, so we can learn the second thing you have to know about shutdown. What's this? It still doesn't run? Nope. Doesn't budge. But at least this time it provides a list of arguments you can add to the command line. On my system, that list looks like this:

Usage:    shutdown [-akrhfnc] [-t secs] time [warning message]
-a: use /etc/shutdown.allow
-k: don't really shutdown, only warn.
-r: reboot after shutdown.
-h: halt after shutdown.
-z: shutdown using software suspend.
-f: do a 'fast' reboot (skip fsck).
-F: Force fsck on reboot.
-n: do not go through "init" but go down real fast.
-c: cancel a running shutdown.
-t secs: delay between warning and kill signal.
** the "time" argument is mandatory! (try "now") **

See that final line in the output? The one marked with two asterisks? That's the second lesson. You have to tell shutdown when to act. The easiest way -- but not the only way -- is simply to specify "now" as the time. As in shutdown now.

When shutdown is entered correctly -- as root, and with a valid time argument -- it does the following.

  • Notifies all users the system is going down
  • Blocks additional logins
  • Notifies all running programs so they can close/save files and exit gracefully
  • Notifies init to change the runlevel

Psssst! Tell init to take us to level X

Let's talk a little bit about what changing the runlevel means, and then about how we can control which runlevel shutdown tells init to go to. The init program is called at boot time and -- among other things -- it controls the state -- or the runlevel -- of the machine. The various runlevels dictate what programs are allowed to be run. The important runlevels are:

  • 0 - Halt the machine
  • 1 - Single-user (maintenance) mode
  • 5 - Normal operating mode
  • 6 - Reboot

We're starting from runlevel 5, so we'll never need to use shutdown to get us there. But we can use the following command line arguments with shutdown to allow us to either reboot or halt the machine at shutdown:

  • -h Halt the machine (runlevel 0)
    -r Reboot the machine (runlevel 6)

If you don't specify either halt or reboot, shutdown assumes you want single-user mode and changes to runlevel 1. Please note that this can leave you with an apparently frozen system if you enter the command from a terminal window in an X session, as you normally will do. So be sure to specify halt or reboot when you enter the command.

About lesson two

There are a couple of ways to comply with the second lesson, telling shutdown when to act. The first is simply to tell shutdown to do it now. Like this example for a reboot:


shutdown -r now

The man page suggests that you can specify a delay of X seconds between the warning message and the change in runlevel. The -t X argument -- where X is the number of seconds to pause -- is supposed to do exactly that. On my SUSE 9.2 system, however, that results in a delay measured in minutes rather than seconds.

I asked shutdown's maintainer, Miquel van Smoorenburg, how to use the -t secs argument. He replied:

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 dig-
its) 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 shutdown and reboot in 60 seconds would be "shutdown -r +1"

So there you have it. Another useful tool at your command. Command line, that is. As always, and with the caveat that the -t secs does not perform as advertised, turn to the man for more info.

Click Here!