Linux

Linux Shutdown Command Example

Hello readers, in this tutorial, we will learn how to shut down a Linux system using the shutdown command.

1. Introduction

The shutdown command in the Linux system powers down the system in a safe and secure manner. During the system power down process, all logged-in users are notified about the system shutdown and the login operations are blocked. This command is commonly used to power down or reboots the local or the remote machines and does its job by signaling the init process. The init process changes the runlevel (i.e. the state) of the operating system where:

  • Runlevel 0 halts the Linux system
  • Runlevel 6 reboots the Linux system
  • Runlevel 1 only allows the administrative tasks


 
The shutdown command has the following prototype form:

Basic syntax

# shutdown [OPTION] [TIME] [MESSAGE]

Where:

  • The OPTION parameter specifies the different set of options for the shutdown command
  • The TIME parameter specifies the time in minutes or hours to perform the shutdown operation. This parameter can be formatted in three ways i.e.:
    • Absolute time in the hh:mm format
    • Minutes format where +m denotes the number of minutes to wait before the final shutdown
    • The word now immediately shut down the system
  • The MESSAGE parameter specifies an alert message for the users

1.1 Shut down Options

The below table lists the different options that can be used with the shutdown command:

OptionsDescription
-aThis option controls the access to the shutdown command by preventing the system power down if an authorized user is logged-in
-kThis option sends the reel warning messages for the system shutdown and disable the logins, but does not actually power down the system
-rThis option will reboot the Linux system after the shutdown
-hThis option instructs the system to perform a shutdown and then halt the Linux system
-PThis option instructs the system to perform a shutdown and then power down the Linux system
-cThis option cancels a pending shutdown but it doesn’t apply to the shutdown –h now command

2. Practical usage

Let’s understand the implementation and the usage of this command with the help of the sample snippets.

2.2.1 Start Linux

Start a standalone Linux instance as shown below.

Fig. 1: Start Linux instance
Fig. 1: Start Linux instance

2.2.2 Instant Shut down

Developers can immediately power down the Linux machine by using the now option in-conjugation with the shutdown command. This command will notify the logged-in users and immediately powers down the Linux system. The following Linux snippet can be used.

Query 1

# shutdown -h now

The command gives the below output.

Broadcast message from avengers@thanos-laptop
	(/dev/pts/1) at 10:30 ...

The system is going down for halt NOW!

2.2.3 Shut down machine with the User-defined Message

Developers can immediately power down the machine by displaying a custom message. This message will be appended to the broadcast message and the following Linux snippet can be used.

Query 2

# shutdown -h now 'You can’t escape… System shutdown still arrives! Smile….'

The command gives the below output.

Broadcast message from avengers@thanos-laptop
	(/dev/pts/1) at 10:35 ...

The system is going down for halt NOW! You can’t escape… System shutdown still arrives! Smile….

2.2.4 Scheduling the shutdown

Developers can also schedule the system power down by specifying the time parameter. Let’s say administrator want to give the logged-in users ‘20 mins‘ before the system goes for the shutdown. The following Linux snippet can be used.

Query 3

# shutdown -h +20

The command gives the below output.

Broadcast message from avengers@thanos-laptop
	(/dev/pts/3) at 10:40 ...

The system is going down for a halt in 20 minutes!

2.2.5 Schedule the system reboot

The Linux shutdown command can also be used to reboot the system with the -r option. The Query 4 snippet specifies a system reboot at a given time.

Query 4

# shutdown -r 13:40 'System reboot at 13:40 PM. Please save your work ….!'

The command gives the below output.

Broadcast message from avengers@thanos-laptop
	(/dev/pts/3) at 10:45 ...

The system is going down for a reboot in 180 minutes! System reboot at 13:40 PM. Please save your work ….!

2.2.6 Cancelling the shutdown

Developers can cancel the scheduled shut down by using the -c option in-conjugation with the shutdown command. The following Linux snippet can be used.

Query 5

# shutdown -c

The command gives the below output.

Broadcast message from avengers@thanos-laptop
	(/dev/pts/3) at 10:55 ...

shutdown: Shutdown canceled

2.2.7 Shutdown Logs

Consider a scenario where developers want to display the log details of all shutdown since the log file was created in the Linux system. The following Linux snippet can be used.

Query 6

# last -x shutdown

The command gives the below output.

shutdown system down  2.6.32-131.12.1. Sun Jan  1 15:35 - 15:37  (00:02)
shutdown system down  2.6.32-131.12.1. Sun Mar 28 17:53 - 18:00  (00:06)
shutdown system down  2.6.32-131.12.1. Sat Apr 27 15:21 - 15:23  (00:02)

Here developers must be thinking the difference between halt and power off. Halt only shut down the operating system while in Power off, the operating system instructs the power interface to send a signal to the power unit to completely switch off the system. That’s all for this post. Happy Learning!!

3. Conclusion

In this tutorial, developers learned how to shut down the system in a secure manner. We can power down the machine instantly or schedule it using the 24-hour format. Do remember, the user with the root access can only execute the shutdown command. For more help on the shut down command in Linux, developers can use the below command:

shutdown --help

You can download the sample commands in the Downloads section.

4. Download the Eclipse Project

This was a tutorial of shutdown commands in Linux.

Download
You can download the full source code of this example here: Linux_Shutdown_Cmds

Yatin Batra

Yatin has graduated in Electronics & Telecommunication. During his studies, he has been involved with a large number of projects ranging from programming and software engineering to telecommunications analysis. He works as a software developer in the information technology sector where he is mainly involved with projects based on Java and J2EE technologies platform.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button