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.

up vote 6 down vote accepted

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.

  • 1
    Assuming you are using a Qwerty keyboard, yes, that's true, but the OP asked for a command to (basically) kill his/her system :P – CijcoSistems Jul 3 '14 at 9:14
  • 1
    It's a command if you tell somebody to do it. I hath commandeth! (I've also added mildly safer command versions). – Oli Jul 3 '14 at 9:15
  • I agree with the impossibility of running E and/or I due to the "nuke". – CijcoSistems Jul 3 '14 at 9:16

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"
  • 1
    Quote from CentOS Documentation: To echo values to this file, the /proc/sys/kernel/sysrq must be set to a value other than 0. In addition, I would strongly suggest to echo s > /proc/sysrq-trigger before that so the system syncs discs and the documents you just saved are really written to non-volatile media. Otherwise they could be in RAM waiting to be written when you suddenly halt the system in such a hard way. – CijcoSistems Jul 3 '14 at 9:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.