major.io

Words of wisdom from a Linux engineer focused on information security

major.io

Words of wisdom from a systems engineer

  • Who am I?
  • icanhazip FAQ
  • Résumé
  • Keybase
  • RSS
Creative Commons License

Linux: emergency reboot or shutdown with magic commands

January 29, 2009 By Major Hayden 43 Comments

Most linux distributions use some type of mechanism to gracefully stop daemons and unmount storage volumes during a reboot or shutdown. It’s most commonly done via scripts that will wait for each daemon to shut down gracefully before proceeding to the next daemon.

As we know, sometimes servers misbehave due to things put them through, and you can quickly end up in a situation where things are going badly. I’m talking about the type of situation where you’re connected via SSH to a server that controls phone lines for five million people and it sits in a tiny building 400 miles away from the nearest human being. We’re talking bad. If you issue a plain reboot command, it might not even make it that far. Once SSH stops running, you’re going to be out of luck.

If you find yourself in this situation (and I hope you won’t!), you have some options to get your way with a misbehaving server remotely. You can force an immediate reboot with the following:

XHTML
1
2
echo 1 > /proc/sys/kernel/sysrq 
echo b > /proc/sysrq-trigger

WHOA THERE! This is pretty much the same as pressing the reset button on the server (if equipped). No daemons will be shut down gracefully, no filesystem sync will occur, and you may get the wrath of a fsck (or worse, a non-booting server) upon reboot. To do things a little more carefully, read on.

These are called magic commands, and they’re pretty much synonymous with holding down Alt-SysRq and another key on older keyboards. Dropping 1 into /proc/sys/kernel/sysrq tells the kernel that you want to enable SysRq access (it’s usually disabled). The second command is equivalent to pressing Alt-SysRq-b on a QWERTY keyboard.

There’s a better way of rebooting a misbehaving server that Wikipedia shows with the mnemonic “Reboot Even If System Utterly Broken”:

XHTML
1
2
3
4
5
6
unRaw      (take control of keyboard back from X),  
tErminate (send SIGTERM to all processes),
kIll      (send SIGKILL to all processes),
  Sync     (flush data to disk),
  Unmount  (remount all filesystems read-only),
reBoot.

I can’t vouch for this actually working, but I’m interested to try it. UPDATE: I’ve been told that doing this series of commands with ReiserFS is a very bad idea.

If you want to shut the machine down entirely (please think about it before using this on a remote system):

XHTML
1
2
echo 1 > /proc/sys/kernel/sysrq 
echo o > /proc/sysrq-trigger

If you want to keep SysRq enabled all the time, you can do that with an entry in your server’s sysctl.conf:

XHTML
1
kernel.sysrq = 1

Share this post:

  • Twitter
  • Google
  • LinkedIn
  • Reddit
  • Email
  • Print

Tagged With: command line, emergency, kernel, linux, sysctl

Comments

  1. Dan C says

    January 30, 2009 at 4:23 am

    Good post!

    Reply
  2. W says

    January 30, 2009 at 5:44 am

    “I can vouch for this actually working, but I’m interested to try it.” — this sentence doesn’t make much sense.

    Reply
  3. Kyle says

    April 23, 2009 at 10:50 am

    With modern linuxes, I’ve never had an issue using:

    sync
    sync
    reboot -t 0

    Over SSH.

    Reply
  4. chrisd says

    May 27, 2009 at 11:10 am

    @Kyle: On machines with defunct or dead processes (say a hung NFS mount or disk full problems) “reboot” will not always work. sysrq is a last resort but a welcome one for those rare cases.

    Reply
  5. Morgan Tocker says

    July 21, 2009 at 9:09 pm

    One of the useful things to do with this tip, is to use it to test your HA setup (such as with MySQL & DRBD / Heartbeat) worked as expected.

    It’s always interesting to know that you got the data back that you were supposed to – and you managed to solve the durability problem that is the ‘D’ in ACID.

    Reply
  6. vinsane says

    May 18, 2010 at 12:26 pm

    Note: you don’t need to turn system request on if you’re echoing into sysrq-trigger, it will always work as long as you are root. You only need to turn it on for key combos.

    Reply
  7. Ac says

    January 5, 2011 at 5:22 pm

    Thank you! This worked when shutdown -r now, reboot, and init 6 wouldn’t. One of the disks failed and the system was locked up forever trying to unmount it. Fortunately it was just a backup disk and no data was lost on the others.

    Reply
  8. hwertz says

    April 1, 2011 at 12:45 am

    “With modern linuxes, I’ve never had an issue using:

    sync
    sync
    reboot -t 0

    Over SSH.”
    I just did! No 5 million user phone switch here, but a remote box at my parents house, I just logged in and it was good and screwed (it’s a bad sign when you see “/bin/ls: I/O error”. Reboot command got me “reboot: command not found”. It seems /dev/sda dropped right off the SATA bus. Of course, I *SERIOUSLY* doubt it’s going to sucessfully reboot (probably, the disk has either crashed, or will need an actual power cycle to show back up) but at least I could attempt a reboot with these instructions, the machine was doing nothing as it stood.

    Reply
  9. Willow Wright says

    May 29, 2012 at 11:54 am

    What happens if you cannot access the Website/server remotely, either by SSH or NX….?

    Our servers are down in the Core of our TV station, and unless I’m out of town, I COULD drive in and stand in front of the server…So….the server (physical machine) is still running, but I can’t access the Website. Sometimes, can’t log in at all.

    Up till now, I’ve just hit the power switch, and gotten yelled at by my Linux mentor….but, if everything seems to be locked up, what do you do? Reset has been unsuccessful….guess there isn’t one on this machine.

    If the machine is accessible, at least at the console….standing in front of the machine..and yet my Website is totally inaccessible, non-responsive, what is the proper procedure for restarting the server?

    Thanks,

    Reply
  10. agu says

    August 21, 2012 at 7:49 am

    Good input. Thanks!

    Reply
  11. tsoft says

    November 3, 2012 at 3:31 pm

    Man!!! You are my personal Jesus Christ!!!!!
    Though my little zombie Linux was only 150 km far.

    Reply
    • Backuptron says

      July 23, 2015 at 8:50 am

      My zombie was in a different country. It worked like a dream :D

      Reply
  12. Kev says

    July 11, 2013 at 6:05 am

    Wow this is really very useful!
    I had a linux system in a state – mounted systems hanging. Reboot was doing nothing.
    Performed your little trick and voila it was back up and synced in no time. What i see in the logs are

    Jul 11 03:28:30 fstab-sync[4289]: removed all generated mount points
    Jul 11 03:28:32 fstab-sync[4403]: added mount point /media/cdrom for /dev/hdb

    Very tidy!

    Reply
  13. Su says

    September 10, 2013 at 12:32 pm

    Hi….im trying to shutdown by just entering ‘shutdown’.But its not working.It says:
    shutdown: time expected
    Try ‘shutdown –help’ for more information
    Can anyone please help?

    Reply
    • AceStar says

      September 1, 2015 at 6:53 pm

      shutdown requires a time, you want shutdown now normally to shutdown immediately, unless you want to give logged in shell users a warning and countdown. If it’s a remote server make sure you have a way to bring the system back up.

      Reply
  14. Todd says

    December 27, 2013 at 11:59 pm

    YOU JUST SAVED MY LIFE! Thank you! In Ohio with a production server in Philadelphia. It came back up!

    Reply
  15. E71 says

    April 8, 2014 at 6:42 am

    Thanks for this. I’ll keep this info handy the next time my server says “Shutting down now” after I issue a ‘shutdown -h now’ but nothing happens.

    Reply
  16. Mustafa says

    July 31, 2014 at 3:05 pm

    Saved life. Remote server was over loaded, 8 cores, but system load > 20 due to a stupid bug in a program. Could not spawn new process. Executed this magic. Since echo is a shell built-in it did not spawn another process, and saved my life. Thanks.

    Reply
  17. Arjun says

    October 9, 2014 at 11:18 pm

    Thanks,
    Been searching for such a command .. It always works.

    Reply
  18. Hassan says

    March 15, 2015 at 7:35 am

    Thanx Major, this was extremely useful.

    Reply
  19. Basavaraju M says

    March 21, 2015 at 7:01 am

    Thanks Friend,

    one of our production box was not rebooting, from your magic words server rebooted on a fly.

    Regards
    Basavaraju.M

    Reply
  20. Jamie Carl says

    March 31, 2015 at 6:40 pm

    Brilliant! This little trick just got me out of a pretty bad jam with a remote server that refused to reboot gracefully.

    Reply
  21. Prateek Baranwal says

    April 23, 2015 at 9:33 am

    Your a savior!!! The server got in a bad state with the / mount acting up.

    Reply
  22. Paul Bygrave says

    May 5, 2015 at 10:05 am

    Many thanks, command just saved me a trip to the data centre 20 minutes before home time.

    Reply
Newer Comments »

Trackbacks

  1. 8 Bit Pipe » Magic SysRq says:
    May 18, 2010 at 3:32 pm

    […] I was doing some troubleshooting on a server and, as a joke, a friend sends me an article to the magic shutdown commands for a solution. I found the article interesting and, once I got home, proceeded to blow up my […]

    Reply
  2. Eugene Kravtsoff Blog » Blog Archive » https://rackerhacker.com/2009/01/29/linux-emergency-reboot-or-shutdown-with-magic-commands/ says:
    December 2, 2010 at 5:12 pm

    […] Linux: emergency reboot or shutdown with magic commands […]

    Reply
  3. “bus error” when trying to reboot Linux - Admins Goodies says:
    August 22, 2011 at 10:40 am

    […] emergency reboot or shutdown with magic commands for more […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.