How to Use Linux Shutdown and Reboot Command with Examples

This tutorial explains various command line options available for the Linux shutdown and reboot commands.

Linux shutdown command options

Syntax:

shutdown [OPTION]… TIME [MESSAGE]

From the syntax we see that besides OPTIONS and TIME, there is a MESSAGE that can be produced on the command line through this command. Please note that you need to have root privileges to execute this command.

1. A basic example

In its very basic form, the shutdown command can be used as :

 $ sudo shutdown 5 [sudo] password for admin: Broadcast message from admin@dev-db (/dev/pts/2) at 19:44 ... The system is going down for maintenance in 5 minutes!

So we see that shutdown command initiated the process of system shutdown.

2. Initaite the reboot process through -r option

The option -r can be used to to initiate system reboot through the shutdown command.

Here is an example :

 $ sudo shutdown -r 5 Broadcast message from admin@dev-db (/dev/pts/2) at 19:49 ... The system is going down for reboot in 5 minutes!

So we see that system reboot was initaited.

3. Poweroff or halt off the system using -h option

This option lets the system decide to either power off or halt the system once it’s brought down.

Here is an example :

 $ sudo shutdown -h 5 Broadcast message from admin@dev-db (/dev/pts/2) at 19:51 ... The system is going down for halt in 5 minutes!

So we see that system decided to halt when -h option was passed to shutdown command on my machine.

4. Insist the system to Power off using -P option

The -P option lets the user to insist system to power off once it’s brought down.

Here is an example :

 $ sudo shutdown -P 5 Broadcast message from admin@dev-db (/dev/pts/2) at 19:53 ... The system is going down for power off in 5 minutes!

So we see that system prepared to power off.

5. Print a message using shutdown command

User can also print a custom message through shutdown command. This could be helpful when the the user or admin wants to convey to all the currently logged in users the reason of shutdown.

Here is an example :

 $ sudo shutdown 5 "System is shutting down to install new updates" Broadcast message from admin@dev-db (/dev/pts/2) at 19:56 ... The system is going down for maintenance in 5 minutes! System is shutting down to install new updates

So we see that the message was displayed as the last line in the output above.

6. Insist the system to be halted using -H option

Shutdown command provides an option -H through which the user/admin can insist the system to be halted once brought down.

Here is an example :

 $ sudo shutdown -H 5 Broadcast message from admin@dev-db (/dev/pts/2) at 19:58 ... The system is going down for halt in 5 minutes!

7. Cancel the shutdown operation using -c option

If due to any reason the shutdown needs to be cancelled within the time mentioned in the shutdown command, the -c option can be used to cancel the shutdown.

Here is an example :

 $ sudo shutdown -c

Running the above command in a separate shell window affects an ongoing shutdown in the following way :

 $ sudo shutdown 5 Broadcast message from admin@dev-db (/dev/pts/2) at 19:59 ... The system is going down for maintenance in 5 minutes! shutdown: Shutdown cancelled

So we see that shutdown was canceled.

8. Send out warnings and disable logins using -k option

The -k option provided by the shutdown command does not actually halts or power off’s the system but just sends the warnings and disables the warnings.

Here is an example :

 $ sudo shutdown -k 5 Broadcast message from admin@dev-db (/dev/pts/2) at 20:04 ... The system is going down for maintenance in 5 minutes!

So we see that just a warning is produced but no shutdown took place after 5 minutes.

Linux reboot command options

The reboot command allows the system administrator to reboot a system. This command is kind of a front end to shutdown command only except when it is run with –force option or when the runlevel is 0 or 6. In these two cases, this command invokes the reboot() system call to perform the desired operation.

Syntax for reboot command:

 reboot [OPTION]...

So we see that unlike shutdown command discussed above, reboot command neither accepts any time argument nor any message argument. It simply reboots your system the moment it is run.

1. Reboot your system

This can be done by simply running the reboot command with root privileges.

Here is the example :

 $ reboot

2. Initiate forced reboot using -f option

This option invokes the reboot() system call leading to non-graceful reboot of the system.

Here is an example :

 $ reboot -f

3. The /var/run/wtmp and /var/run/utmp files

While dealing with reboot command, one must have the knowledge of these files :

/var/run/utmp : Where the current runlevel will be read from; this file will also be updated with the runlevel record being replaced by a shutdown time record.

/var/log/wtmp : A new runlevel record for the shutdown time will be appended to this file.

A dummy update to /var/log/wtmp can be done using -w option.

 $ sudo reboot -w [sudo] password for admin: 

4. Check the update status of /var/log/wtmp

As /var/log/wtmp is not a readable file so it’s update status could be checked using the cksumcommand. This command should show an updated checksum when reboot -w is run.

Here is an example. From the following output, we can see that the checksum for the file changed, after we did the reboot -w.

 $ cksum /var/log/wtmp 5957036 402048 /var/log/wtmp $ sudo reboot -w $ cksum /var/log/wtmp 978698526 402432 /var/log/wtmp
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s