How to emergency restart or reboot or shutdown Linux servers?
- May 24, 2018
- 0
Most often, when you are working with Linux clusters in a remote location, at times you are left with no other options, but for a forceful reboot of the system when there is no GUI, but only shell access is available. As all other tasks in Linux, reboot and shutdown can also be performed from the command line. The commonly used commands to shut down and reboot Linux servers are shutdown, halt, poweroff, reboot, and REISUB keystrokes.
How to emergency restart, reboot or shutdown Linux servers
1. Shutdown command
The command is used to shutdown or restart, both remote as well as local systems.
The syntax is as follows:
shutdown [OPTION] [TIME] [MESSAGE]
The shutdown command can be called like:
# shutdown -h now
h refers to halt
now refers the time, to shut down the system right now
You can also add the countdown time, say 10 minutes before a shutdown by rewriting the command including the time instead of now. For example –
# shutdown -h +10
If the above command is used, a notification message for the shutdown will be flashed to all other users who have logged in, and they can save their work.
The same command can be used to restart as well when called like:
# shutdown -r +10
Please notice that “h” is replaced by “r” here.
If required the shutdown can be canceled at this point by calling the command as below:
# shutdown -c
2. Reboot command
This can also be used to reboot or shut down Linux.
To shutdown, call the command
# reboot -p
p refers to power off
Call the command
# reboot
directly to reboot Linux.
A calm shut down happens to one daemons one by one completely.
To execute a force shut down, as that of pressing the power button of a CPU, call the command
# reboot -f
where f stands for force shut down.
3. Halt command
# halt
This ensures an instant shut down, but a point to be remembered is there are possibilities that the desktop may hang on running this command as there is a behavioral inconsistency.
4. poweroff command
The functioning of this command is almost the same as that of halt command. The command can be called as
# poweroff
5. R E I S U B power strokes
These commands can be used only when you have an absolute control of your system.
The magic SysRq keys (Print screen key)
Let us also think about the other way round, when the system does not respond at all. The only choice left is to press the CPU power button which may lead to the corruption of all the valuable data in your system. The solution introduced is the magic SysRq keys. It has a similar effect as that of a reset button of a computer hardware paired with many options and more control. The magic SysRq keys will not function under certain circumstances, like a kernel panic or a hardware failure which prevents the kernel from a proper running.
The magic SysRq keys are not activated by default on many Linux distributions. We can enable it by adding or modifying the following line immediately after Linux system is installed.
# vi /etc/sysctl.conf
Append following config directive:
kernel.sysrq=1
Save and close the file. Reload settings:
# sysctl -p
Reboot the system so as the changes come into effect.
The magic SysRq keys can be used effectively in emergency situations using the following combination.
ALT+SysRq+COMMAND-KEY
Where the command keys can be b (instantly reboot the system), o (will shut down the system), s (strives to syn all the files), u (try to remount filesystems read-only), e (a SIGTERM is sent to all processes excluding init), h (show help). All the keys need o be hit simultaneously
The following key combinations can reboot your system immediately exactly like executing the reboot-f command or pressing the power button of your CPU.
ALT + PrintScreen + B
Instead of B key, we can use the following keys, R E I S U first, where each key is assigned the following functions. Ensure to leave some time gap between each of the keys.
Key functions:
R: unRaw ( take control of keyboard back from X),
E: tErminate (send SIGTERM to all processes),
I: kIll (send SIGKILL to all processes),
S: Sync (flush data to disk),
U: Unmount (remount all filesystems read-only),
B: reBoot.
Learn all those commands by heart to answer the question how to emergency restart or reboot, so as to have a safe shut down in Linux system keeping all these secrets well understood!