Jul 092015
 

How to do Clean ShutdownLearning how to do a clean shut down of a Linux system is important if you don’t want to mess up your computer.

Just pressing your finger on the power button will certainly shut down or reboot your Linux system. But it can also cause serious damage like data corruption.

Remember, Linux is constantly writing data to disk even if it’s not apparent to you. Also, Linux keeps data in memory although it might appear to have written the data to disk.

So it’s crucial to do an orderly shutdown of a Linux desktop or server.

When you do a clean or orderly shutdown, you’re essentially informing users and processes on the Linux system of the shutdown and blocking new logins.

In this post, we’ll cover various options to properly reboot and shut down a Linux system.

You must be root or use sudo to run the shutdown command.

When I tried to run shutdown without being root or using sudo, I got the following error:

[thomas@localhost ~]$ shutdown
Must be root.

So always make sure you’re root or use the sudo prefix before running the shutdown command.

[thomas@localhost ~]$ sudo shutdown
Shutdown scheduled for Thu 2015-07-09 11:02:17 EDT, use 'shutdown -c' to cancel.
[thomas@localhost ~]$ 
Broadcast message from root@localhost.localdomain (Thu 2015-07-09 11:01:17 EDT):
The system is going down for power-off at Thu 2015-07-09 11:02:17 EDT!

By the way, the commands in this post were tested on a Dell Optiplex 780 running CentOS 7.

Shut Down – Now vs Later

As a systems administrator, you have the choice to shut down a Linux system immediately, a few minutes later or at a specific time that could be several hours later.

We’ll first consider the commands to shut down or reboot a Linux box immediately and then look at how to do it later.

The shutdown and reboot commands are simple and you should be able to grasp them in no time.

Shut Down Now

Either of the two commands should work to shut down your Linux box immediately.

[thomas@localhost ~]$ sudo shutdown now

Alternatively, issue the below command.

[thomas@localhost ~]$ sudo shutdown -h now

When you use shutdown without any options, you might think the Linux box will shut down immediately. Actually, no!

It will shut down after a minute.

Take a dekko below.

[thomas@localhost ~]$ sudo shutdown
Shutdown scheduled for Thu 2015-07-09 11:02:17 EDT, use 'shutdown -c' to cancel.
[thomas@localhost ~]$ 
Broadcast message from root@localhost.localdomain (Thu 2015-07-09 11:01:17 EDT):

The system is going down for power-off at Thu 2015-07-09 11:02:17 EDT!

Shut Down Later

Now we’ll consider how to safely shut down a Linux system later, i.e. at a specified time.

To shut down a Linux, one option is to issue the shutdown -h +n command (n= no of minutes).

If you want to shut down the Linux server after six minutes, you’d issue the following command.

[thomas@localhost ~]$ sudo shutdown -h +6
Shutdown scheduled for Thu 2015-07-09 11:47:52 EDT, use 'shutdown -c' to cancel.

You can also specify the exact time when you want the system to shut down.

Remember to use the 24-hour time format, not the AM or PM format.

In the below command, we’ve specified that the system must shut down at 15:25.

[thomas@localhost ~]$ sudo shutdown -h 15:25
Shutdown scheduled for Thu 2015-07-09 15:25 EDT, use 'shutdown -c' to cancel.

Reboot Linux System Now

More often than not, we’re rebooting rather than shutting down a Linux system.

The following command should enable you to reboot your Linux box cleanly.

[thomas@localhost ~]$ sudo shutdown -r now

Reboot Later

You can always tell the system to reboot later via the shutdown -r +n command where n represents the number of minutes.

So if we wanted to reboot after seven minutes, we’d issue the following command.

[thomas@localhost ~]$ sudo shutdown -r +7
Shutdown scheduled for Thu 2015-07-09 12:09:40 EDT, use 'shutdown -c' to cancel.
[thomas@localhost ~]$ 
Broadcast message from root@localhost.localdomain (Thu 2015-07-09 12:02:40 EDT):

The system is going down for reboot at Thu 2015-07-09 12:09:40 EDT!

Broadcast message from root@localhost.localdomain (Thu 2015-07-09 12:08:40 EDT):

The system is going down for reboot at Thu 2015-07-09 12:09:40 EDT!

If you wanted to reboot the Linux system at 8:23 you’d run the below command.

[thomas@localhost ~]$ sudo shutdown -r 8:23

Cancel a Shut Down

What if you change your mind after issuing a shut down or reboot command.

In some situations, you can cancel the shutdown command.

[thomas@localhost ~]$ sudo shutdown -c

Broadcast message from root@localhost.localdomain (Thu 2015-07-09 11:01:44 EDT):

The system shutdown has been cancelled at Thu 2015-07-09 11:02:44 EDT!

When you cancel a shutdown, you can even include a reason why you decided to cancel it to explain your decision to other users on the network

Here’s an example of a shutdown cancellation with an explanation

[thomas@localhost ~]$ sudo shutdown -c "Sorry, I made a mistake"

Broadcast message from root@localhost.localdomain (Thu 2015-07-09 12:44:13 EDT):

Sorry, I made a mistake
The system shutdown has been cancelled at Thu 2015-07-09 12:45:13 EDT!

The above examples should stand you in good stead when you want to do an orderly reboot or shutdown of a Linux system.

Learning to perform an orderly shut down or reboot of your system are key skills to possess if you intend to get a solid grip on Linux server administration.

Sorry, the comment form is closed at this time.