Terminal commands to hard shutdown and hard restart
I want to do hard restart and hard shutdown(immediate shutdown and restart) through terminal.
Is this possible through terminal commands?
And note that no answers in this How do I shut down or reboot from a terminal? question does immediate shutdown or restart. So it's not a dupe.
Solution 1:
NOTE: Save any working documents before running the below commands.
Terminal command for hard-shutdown,
sudo sh -c "echo o > /proc/sysrq-trigger"
Terminal command for hard-restart,
sudo sh -c "echo b > /proc/sysrq-trigger"
Solution 2:
It would be safer to do a Alt+SysRq+(R,E,I,S,U,B or O) than force a hard reboot.
- R Switch the keyboard from raw mode to XLATE mode
- E SIGTERM everything except init
- I SIGKILL everything except init
- S Syncs the mounted filesystems
- U Remounts the mounted filesystems in read-only mode
- B Reboot the system, or O Turn off the system
You could just Alt+SysRq+B/O to reboot/halt if you really wanted to but you put your filesystems at risk by doing so. Doing all of the above is relatively safe and should work even when the rest of the system has broken down.
This is essentially the same method you're talking about in your commands but I'm not sure you could script the E and I (as they'll nuke your terminal access). But you could definitely handle the disk access and reboot or shutdown.
for i in s u b; do echo $i | sudo tee /proc/sysrq-trigger; sleep 5; done # reboot
for i in s u o; do echo $i | sudo tee /proc/sysrq-trigger; sleep 5; done # halt
You could still lose data from running applications but it shoudn't knacker your filesystem. If you have particularly huge disk write caches it might be best to increase the sleep
value.