Understanding the Linux Shutdown Command

Understanding the Linux Shutdown Command
Page content

The Linux shutdown command executed from within a Graphical User Interface doesn’t really require understanding of what takes place when you are ready to turn off your computer for the day. However, it needs to be understood by people who work in text mode and those who might want to someday become Linux administrators. Actually, there are even other commands that can be used to shut down a UNIX or Linux system such as the poweroff, init, and halt commands. There are differences between them; therefore, there’s some confusion regarding which one should be used.

Other Commands Used for Shutdown

The poweroff command is rarely used and the init command can be avoided- unless you need to control the system’s run level. Incidently, run level 0 itself is halt and causes a shut down, but like poweroff, you really don’t need to issue init just to shut down. Most of the misunderstanding surrounds the question of when to use the halt command and when to use the Linux shutdown command. You might have been told that using halt could be harmful to the system because it doesn’t allow it to run necessary shutdown processes which could cause a loss of data and file corruption, especially if issued during a running procedure such as a back-up. Just as there are certain processes that must run for a system to get up and going, likewise there are processes that need to be killed for it to properly shutdown in order to maintain stability.

Is Use of the Halt Command Safe?

While it’s definitely true that even Linux and UNIX, as stable as they are, need to be shut down properly, it’s not totally correct to say that you always jeopardize a system by using the halt command. The various distributions as well as the versions within a single distribution can have sharp differences. No one can be familiar with all of them so the basics are learned and applied across the board, but this doesn’t erase the need to learn your distro of choice. There are some flavors of Linux, Fedora 10 figures among them, in which the halt command is just as safe to use as the Linux shutdown command because it invokes the proper running of necessary shutdown processes. I’ve heard that the same is true for Centos 5.3 and OpenSUSe 11.1, but my personal experience is with Fedora. The point is that although something could be true most of the time, in most cases, and with most systems, you might have a distro that is an exception to the rule.

I’m certainly not saying that you should dismiss how to properly use the Linux shutdown command because it will always be reliable on any multi-user system whereas the halt command might not. It’s better to be safe than sorry. Also, you should understand the major difference between halt and shutdown. If you have a distro of which you’re sure that issuing halt will engage the standard rundown sequence and you are the only user, you’ve nothing to worry about. If, however, you’re working as an administrator or with administrative privileges as a superuser on a multi-user system, you’ll want to avoid halt and use the safe Linux shutdown command. Instead of taking the computer to a state where it’s ready to be turned off, it should take you to a maintenance or single user state. Once there, you can work until you are then ready for actual shutdown which can be achieved by issuing shutdown -h now or halt if you’re on a system which calls shutdown processes with halt.

It’s for this reason that the Linux shutdown command gives you more options that you can pass to it to “tune” the process. For example, you can specify an exact time when the system should shut down or state an amount of time that should elapse before shut down occurs. You can also include a message, perhaps about maintenance, that will be passed to all users along with the default broadcast. This shows them the courtesy of allowing them time to prepare for shutdown. Obviously, if you’re the only user, you don’t need to notify yourself, but you could certainly practice this useful command even as a single user. If you don’t want to practice, issuing halt is just simpler and doesn’t require stating a time. In fact, you can’t state a time with halt. My Fedora 10 system won’t accept the Linux shutdown command unless I specify a time or an amount of time. I’ll instead get an error message that reads, “Illegal hour value.” This isn’t the case with all versions of all distros. In fact, the shutdown command isn’t even recognized in a shell in Puppy–at least not in the version I’m running. That makes sense; Puppy is a single user system, so the halt command is really all that’s needed, if you must use it. However, you don’t want to use halt to shut Puppy down because it does not call the shutdown processes that need to run and the system will more than likely become unstable after using halt even once.

A Final Note on the Linux Shutdown Command

Note also that the Linux shutdown command can take the -h and -r options among others. The -h option will bring the system to a halt and -r will reboot it. Which is easier to type, halt or shutdown -h. Halt is easier and works fine for a single user system on which no one else needs notification of shutdown. In the screen shot below, you can see that I opened a terminal on my Fedora 10 system, became root, and issued the shutdown command passing it the -h option, a time of 2 minutes in which to halt, and a sample custom message. Notice that the system generated two messages for users. The one alerts that the system will be shutting down in two minutes. After one minute elapsed, users were again alerted when only one more minute remained until shutdown. By the way, it is possible to immediately halt a system with the shutdown command also by passing it a 0 or the word now as in shutdown -h 0 or shutdown -h now. There are also options that can be passed to the halt command. You can also see where I found out information about the halt command by typing halt –help. If you’re not sure about something, let the system help you learn and adjust to its specifics. Sometimes it’s the best teacher.