Table of Contents
- Introduction
- How to use Shutdown command in linux
- How to power off linux machine using shutdown command in linux
- How to cancel the scheduled power off using shutdown command in linux
- How to schedule the shutdown using shutdown command in linux
- How to send Shutdown warning while using shutdown command in linux
- How to use shutdown command with halt option in linux
- How to use shutdown command with reboot option
- How to use reboot command in linux
- How to use the Init command to perform a shutdown or reboot in linux
- How to use the Systemctl command to perform the shutdown and reboot in linux
- Other available options to power off or reboot the linux system
Introduction
If you are wondering what are the command options we have to shutdown or reboot your linux machine if you have only a cli based system ?
No worries this article answers all your questions
How to use Shutdown command in linux
To see all the options we have on the shutdown command you can use the ‘help’ option
[root@discoveringsystems-1 / ]$ shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]
Shut down the system.
--help Show this help
-H --halt Halt the machine
-P --poweroff Power-off the machine
-r --reboot Reboot the machine
-h Equivalent to --poweroff, overridden by --halt
-k Don't halt/power-off/reboot, just send warnings
--no-wall Don't send wall message before halt/power-off/reboot
-c Cancel a pending shutdown
How to power off linux machine using shutdown command in linux
When you execute the following shutdown command with ‘now’ option it will shutdown the system right away without scheduling the shutdown
[root@discoveringsystems-centos /]# shutdown now
Connection to < vm -ip > closed by remote host.
Connection to < vm -ip > closed.
Shutdown command along with the power off option will schedule a power off . By default when you execute the following command it powers off the machine after one minute . For example , we entered the command around 20:56 and we are getting a notification from the system that it has been scheduled to power off the machine at 21:56 . Along with that there will be a broadcast message sent by the root to the other users that there is a power-off scheduled to happen at 21:56.
[root@discoveringsystems-centos /]# shutdown -P
Shutdown scheduled for Fri 2022-07-29 00:21:56 EDT, use 'shutdown -c' to cancel.
[root@discoveringsystems-centos /]#
Broadcast message from root@discoveringsystems-centos.ds.com (Fri 2022-07-29 00:20:57 EDT):
The system is going down for power-off at Fri 2022-07-29 00:21:56 EDT!
How to cancel the scheduled power off using shutdown command in linux
Let’s say you changed your mind upon powering off the machine and looking to cancel it . We can use the cancel option along with the shutdown command to cancel it . As soon as you canceled it you will also see a broadcast message from the root that the system shutdown has been canceled
[root@discoveringsystems-centos /]# shutdown -c
Broadcast message from root@discoveringsystems-centos.ds.com (Fri 2022-07-29 00:21:28 EDT):
The system shutdown has been cancelled at Fri 2022-07-29 00:22:28 EDT!
How to schedule the shutdown using shutdown command in linux
If you would like to schedule the shutdown as per your wish , you can time it like the example below. Here we were asking the system to shutdown in 10 minutes. Time of the command execution is 02:29:27 and the system is scheduled to shutdown at 02:39:27
[root@discoveringsystems-centos /]# shutdown -P 10
Shutdown scheduled for Fri 2022-07-29 02:39:27 EDT, use 'shutdown -c' to cancel.
[root@discoveringsystems-centos /]#
Broadcast message from root@discoveringsystems-centos.ds.com (Fri 2022-07-29 02:29:27 EDT):
The system is going down for power-off at Fri 2022-07-29 02:39:27 EDT!
[root@discoveringsystems-centos /]# date
Fri Jul 29 02:29:27 EDT 2022
How to send Shutdown warning while using shutdown command in linux
If you would like to send only a warning , but not actually trying to power off the system then you can use the ‘warning’ option . In this example a warning message is broadcasted mentioning that the system is going to power off in 1 minute , but you can see that the power off didn’t happen at all even after the scheduled time elapsed
[root@discoveringsystems-centos /]# shutdown -k
Shutdown scheduled for Fri 2022-07-29 00:26:28 EDT, use 'shutdown -c' to cancel.
[root@discoveringsystems-centos /]#
Broadcast message from root@discoveringsystems-centos.ds.com (Fri 2022-07-29 00:25:28 EDT):
The system is going down for power-off at Fri 2022-07-29 00:26:28 EDT!
Even after one minute is over the power off didn’t happen
[root@discoveringsystems-centos /]#
[root@discoveringsystems-centos /]# date
Fri Jul 29 00:26:40 EDT 2022
[root@discoveringsystems-centos /]# date
Fri Jul 29 00:26:46 EDT 2022
How to use shutdown command with halt option in linux
Halting means instructing to stop the cpu functions. Powering off is sending an ACPI command to the PSU to disconnect main power.
[root@discoveringsystems-centos /]# shutdown -H
Shutdown scheduled for Fri 2022-07-29 01:58:43 EDT, use 'shutdown -c' to cancel.
[root@discoveringsystems-centos /]#
Broadcast message from root@discoveringsystems-centos.ds.com (Fri 2022-07-29 01:57:43 EDT):
The system is going down for system halt at Fri 2022-07-29 01:58:43 EDT!
How to use shutdown command with reboot option
We can use the same shutdown command to perform the reboot using the reboot option along with that . In this example below along with the reboot option i also tried to use the no-wall option , which asks the system not to send the broadcast message like it used to do before
[root@discoveringsystems-centos /]# shutdown -r --no-wall
Shutdown scheduled for Fri 2022-07-29 02:10:37 EDT, use 'shutdown -c' to cancel.
Now I tried without the no-wall option and I see the broadcast message
[root@discoveringsystems-centos /]# shutdown -r
Shutdown scheduled for Fri 2022-07-29 02:13:21 EDT, use 'shutdown -c' to cancel.
[root@discoveringsystems-centos /]#
Broadcast message from root@discoveringsystems-centos.ds.com (Fri 2022-07-29 02:12:21 EDT):
The system is going down for reboot at Fri 2022-07-29 02:13:21 EDT!
To cancel the scheduled reboot you can use the cancel option
[root@discoveringsystems-centos /]# shutdown -c
Broadcast message from root@discoveringsystems-centos.ds.com (Fri 2022-07-29 02:12:27 EDT):
The system shutdown has been cancelled at Fri 2022-07-29 02:13:27 EDT!
Scheduling the reboot after 10 minutes
[root@discoveringsystems-centos /]# shutdown -r 10
Shutdown scheduled for Fri 2022-07-29 02:40:13 EDT, use 'shutdown -c' to cancel.
[root@discoveringsystems-centos /]#
Broadcast message from root@discoveringsystems-centos.ds.com (Fri 2022-07-29 02:30:13 EDT):
The system is going down for reboot at Fri 2022-07-29 02:40:13 EDT!
How to use reboot command in linux
To see all the options we have on the reboot command you can use the ‘help’ option
[root@discoveringsystems-centos /]# reboot --help
reboot [OPTIONS...] [ARG]
Reboot the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
How to do Immediate reboot using reboot command in linux
Here we can see that the system goes down right away and the ssh connection we have will be stuck after a few seconds we will get the connection reset message. Here the host abruptly goes down even without closing the TCP connection with the ssh client , our client will retransmit the ssh packets to the host and gives up at one point
[root@discoveringsystems-centos /]# reboot --force
.
.
.
Connection reset by <vm -ip > port 22
How to do normal reboot using reboot command in linux
Let’s do a normal reboot and observe the changes, Here we can see the host when going down for a reboot sends a TCP RESET for the active ssh connection towards the ssh client
[root@discoveringsystems-centos /]# reboot
Connection to <vm -ip > closed by remote host.
Connection to <vm -ip > closed.
How to use the Init command to perform a shutdown or reboot in linux
If you look at the options provided by the init command , we see the options available for the power off and the reboot
[root@discoveringsystems-centos /]# init --help
init [OPTIONS...] {COMMAND}
Send control commands to the init daemon.
--help Show this help
--no-wall Don't send wall message before halt/power-off/reboot
.
.
Commands:
0 Power-off the machine
6 Reboot the machine
2, 3, 4, 5 Start runlevelX.target unit
1, s, S Enter rescue mode
q, Q Reload init daemon configuration
u, U Reexecute init daemon
How to power off using Init command in linux
The following command execution is ‘shutdown -P’ equivalent . But the difference here is, it won’t schedule a power off after 1 minute by default instead it does it right away
[root@discoveringsystems-centos /]# init --help
init [OPTIONS...] {COMMAND}
.
.
Commands:
0 Power-off the machine
[root@discoveringsystems-centos /]# init 0
Connection to <vm -ip > closed by remote host.
Connection to <vm -ip > closed.
How to reboot using Init command in linux
The following command execution is ‘reboot’ command equivalent
[root@discoveringsystems-centos /]# init --help
init [OPTIONS...] {COMMAND}
.
.
Commands:
6 Reboot the machine
[root@discoveringsystems-centos /]# init 6
Connection to <vm -ip > closed by remote host.
Connection to <vm -ip > closed.
How to use the Systemctl command to perform the shutdown and reboot in linux
If you look at the options provided along with the systemctl command , we were able to find some to perform the poweroff , reboot , etc .
[root@discoveringsystems-centos /]# systemctl --help
.
.
.
System Commands:
is-system-running Check whether system is fully running
default Enter system default mode
rescue Enter system rescue mode
emergency Enter system emergency mode
halt Shut down and halt the system
poweroff Shut down and power-off the system
reboot [ARG] Shut down and reboot the system
kexec Shut down and reboot the system with kexec
exit Request user instance exit
switch-root ROOT [INIT] Change to a different root file system
suspend Suspend the system
hibernate Hibernate the system
hybrid-sleep Hibernate and suspend the system
Trying the Power off equivalent
[root@discoveringsystems-centos /]# systemctl poweroff
Connection reset by <vm -ip > port 22
Trying the Reboot equivalent
[root@discoveringsystems-centos /]# systemctl reboot
Connection to <vm -ip > closed by remote host.
Connection to <vm -ip > closed.
Using the systemctl command you can also do other options like Hibernate , suspend , hybrid sleep and also can enter into the modes like rescue mode , emergency mode
Hibernate is a deeper sleep , where the system saves the state by saving the contents of its memory to a file , when you boot up again , it load the state based on the file , Hibernate uses less power than sleep and power usage nearly equivalent to when the system is in shutdown state .State is saved and then goes down
[root@discoveringsystems-centos /]# systemctl hibernate
Connection reset by <vm -ip > port 22
Likewise you can also do the halt , hybrid-sleep . Hybrid-sleep mode is a mode which it works like hibernate + sleep , where its saves the state of the system by saving the contents of the memory to a file and then goes for a sleep
Other available options to power off the linux system
We also have few other options to trigger a power off and reboot , however the following are the least recommended ways to be used in the production environment. Clean ways to perform the reboot and the shutdown are already discussed in the earlier sections.
Using System request key way
System request key is the key combination for which the kernel will respond immediately ,until it is completely locked up . But it needs to be enabled , once enabled we can either do a magical key combination or set things directly on the /proc/sysrq-trigger file . To check whether it is enabled we can take a look at ‘/proc/sys/kernel/sysrq’
[root@discoveringsystems-centos /]# cat /proc/sys/kernel/sysrq
16
Here we can see that the value of 16 is set for /proc/sys/kernel/sysrq
0 – means disabled
1 – enable all sysrq functions
>1 bitmask of allowed sysrq functions
2 – enable control console logging level
4 – enable control of keyboard
8 – enable debugging dumps of processes
16- enable sync command
Lets trigger a reboot by setting the value ‘b’ on ‘/proc/sysrq-trigger` , ‘b’ means reboot the system without syncing or unmounting the disks
[root@discoveringsystems-centos proc]# sudo echo b > /proc/sysrq-trigger
Connection reset by < vm - ip > port 22
Triggering a reboot by causing the Kernel Panic
To check on the current setting of kernel.panic behavior you can use the following command. ‘0’ is the default setting , which means when there is a kernel panic the system doesn’t reboot , but halts
[root@discoveringsystems-centos /]# sudo sysctl --value kernel.panic
0
We can change this behavior by asking it to reload instead of halting by setting the value as ‘1’ instead of ‘0’ , now the system reboots in one second after a kernel panic
[root@discoveringsystems-centos /]# sudo sysctl kernel.panic=1
kernel.panic = 1
Now let’s cause a Kernel panic using the sysrq key value ‘c’ . This sysrq trigger will cause a system crash by a NULL pointer dereference and triggers Kernel panic. The system reboots after one second because of the setting we did earlier . A crash dump will also be collected if configured.
[root@discoveringsystems-centos /]# sudo echo c >/proc/sysrq-trigger