Monday, April 29, 2024
9
rated 0 times [  9] [ 0]  / answers: 1 / hits: 15992  / 2 Years ago, thu, june 16, 2022, 3:32:01

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.


More From » command-line

 Answers
1

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.


[#24309] Friday, June 17, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
variark

Total Points: 82
Total Questions: 114
Total Answers: 122

Location: Sweden
Member since Mon, May 8, 2023
1 Year ago
variark questions
Wed, Nov 3, 21, 13:30, 3 Years ago
Sun, Jan 8, 23, 16:05, 1 Year ago
Thu, Dec 15, 22, 02:10, 1 Year ago
Sun, Jun 26, 22, 12:20, 2 Years ago
Tue, Dec 14, 21, 15:40, 2 Years ago
;