Shutting Down the FreeBSD System


As mentioned in Chapter 2, "Installing FreeBSD," it is very important that you always shut down a FreeBSD system (or any other UNIX system) properly before turning off the power. Failure to do so can result in serious damage to the filesystem. Disks in UNIX are treated as though they contain filesystems that are maintained in a very orderly fashion; if you shut down Windows abruptly by pulling the power cord, the disks will have some fragmented files lying around after it reboots, but the system can cope with them. However, FreeBSDlike most UNIX systemswrites data to the disk in such a way that fragmentation is kept to an almost insignificant level, but an abrupt loss of power can cause the system to actually lose data that was active at the time. FreeBSD has a mechanism called Soft Updates to help combat this problem; see Chapter 12 for more details on using Soft Updates.

Using the shutdown Command

The normal way to shut down the system is with the shutdown command, which you first read about in Chapter 2 in the section titled "Shutting Down FreeBSD." Before you actually shut down the system, however, take a moment to learn about several other options for using this command.

The basic syntax of the shutdown command is as follows:

shutdown option when broadcast message


The first argument tells shutdown what to do, the second argument tells shutdown when to do it, and the third argument tells shutdown the message it should send to all logged-in users. Table 4.1 lists the options that can be used for option in shutdown and what they do.

Table 4.1. The Options for shutdown

Option

Result

Nothing

Kicks everybody off the system and brings the system down to single-user mode with no network support. This is rude to the users, but quick and efficient.

-h

Halts the system.

-p

Halts the system and turns off the power (if the system supports automatic power-off and the kernel is configured to support power management).

-r

Reboots the system.

-k

Kicks everybody off the system and disables any further logins (except from the root user). This option leaves the system in multiuser mode and connected to the network, though. This is useful for occasions when you need to do emergency maintenance but require network access while you do it.

-o

Shuts down without sending a signal to init. This is not usually a good idea because it prevents program-specific shutdown scripts from running.

-n

If the -o option has also been specified, this option prevents the filesystem cache from being flushed before the shutdown. This is probably never a good idea because it can cause data loss.


The "when" part of shutdown can be specified in several ways. As you learned in Chapter 2, the keyword now tells shutdown to perform the action immediately. shutdown also recognizes the format +n, where n is the number of minutes shutdown should wait before performing the action (this gives users time to save their files and close their programs). You can use the format yymmhhmm to specify an exact time when the action should be performed. In this case, yy is the year, mm is the month, hh is the hour (in a 24-hour format), and mm is the minute. If the year and month parts are eliminated, shutdown assumes that the shutdown should happen today. If you specify a time that has already past, shutdown will complain.

The "broadcast message" option enables you to enter a shutdown-notification message that will be broadcast at regular intervals to all logged-in users. This message starts to appear 10 minutes before the impending shutdown and is broadcast more frequently as the shutdown time gets closer.

The following is a step-by-step walkthrough of a typical shutdown process that, in this case, uses a 10-minute delay (don't enter this command now if you don't actually want the system to shut down in ten minutes):

# shutdown -h +10 Hard disk needs to be replaced


This starts the shutdown command and runs it in the background. The system will halt in 10 minutes. In addition, the following broadcast message is displayed on all users' terminals:

*** System shutdown message from root@simba.somewhere.com *** System going down in 10 minutes Hard disk needs to be replaced


Five minutes before the impending shutdown (or immediately, if you specified a delay of less than five minutes), a file called /var/run/nologin will be created automatically by the shutdown program. This file disables any further logins and displays the shutdown message contents when someone attempts to log in. The shutdown command places the time of the shutdown and the broadcast message in this file. For example, in this case, anyone who attempts to log in will see the following:

NO LOGINS: System going down at 17:57 Hard disk needs to be replaced


When the countdown clock runs out, the following actions are performed:

  • A TERM signal is sent to init, which ceases creating any new processes.

  • init reads the file /etc/rc.shutdown and runs any program-specific shutdown scripts it contains.

  • All processes are sent a TERM signal and given time to terminate themselves gracefully.

  • Any processes that did not respond to the TERM signal in a reasonable amount of time are sent a KILL signal, which cannot be ignored. This will force the processes to terminate ungracefully.

  • Cache data is written out to the filesystems with the sync command, the filesystems are dismounted, and the clean flag is set.

  • The kernel is halted.

In addition, shutdown writes an entry in the system log noting the time of the shutdown and who performed the shutdown.

Note

You need to be the root user to shut down the systemeither by logging in directly as root or by using the su command to become root. Another method is the sudo command, which allows certain users to perform one-time administrative tasks.


To shut down your system now, you need to be root, so type su (for "super-user") and press Enter. This command gives you temporary root privileges. Enter the root password when prompted. If you receive a message complaining that you are not in the proper group to use su, log out and then log back in directly as root. Then, assuming there are no other users logged in to the system, issue the following command:

# shutdown -h now


As mentioned in Chapter 2, on a multiuser system, you would not normally shut down the system like this. Instead, you would give users some warning about the impending shutdown.

After the shutdown has completed, you will see the following message:

System halted Please press any key to reboot


Then, and only then, is it safe to turn off your system.

Notes on halt and reboot

Two other commands can be used to halt and reboot the system: halt and reboot, respectively. However, I do not recommend that you get in the habit of using them. Neither of these commands runs the rc.shutdown script, which can cause some programs to terminate improperly. Also, neither of these programs allows you to specify a delay, and neither gives users any warning about the impending shutdown. Therefore, you should always use the shutdown command to halt the system.

However, these two programs do have their uses. You can use halt to make the system 100-percent safe to power down after you have shut down all multiuser processes through the shutdown command. The reboot command is also handy if you're working in singleuser mode; it simply kills all running processes with a SIGKILL (a nonignorable signal) and reboots immediately. These commands are helpful for saving time when it's safe to use them, but those occasions are rare.

Note

If you've used DOS or Windows in the past, you might be in the habit of using the Ctrl+Alt+Delete key combination to reboot a system. By default, FreeBSD will trap the signal sent by this keystroke and perform the equivalent of running the reboot command. This can be a problem when normal users have access to the server's keyboard because it allows them to reboot the system without being root.





FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net