Until today, I thought the SysRq-Key (like the Scroll Lock Key) is one of the useless keys on a modern keyboard. Today I got asked if modern operating systems handle the startup triggered by a reboot different from a cold boot.
While I didn’t find an answer to the initial question (Wikipedia to the rescue) I stumbled upon this.
Basically, the Linux kernel is permanently waiting for special key events. Whenever you press Alt + SysRq + B, your machine will immediately reboot. This is nice to know, but not really “save” as it doesn’t gracefully stop the running programs and also doesn’t finish writing to disk. So we have to tell the Kernel to do exactly this.
The Wikipedia mentions a nice acronym to remember the keystrokes.
Reboot Even If System Utterly Broken
Slowly pressing those keys together with Alt + SysRq will trigger the following events.
R — will take keyboard control away from X.
E — will send a so-called SIGTERM, this is the request the kernel sends whenever a program should stop gracefully. You could imagine this as a bouncer asking you to leave.
I — will go one step further, it will send SIGKILL, this signal tells the process to just go away, imagine the bouncer violently throwing you out. This will close all processes which are hung up or refuse to shut down.
S — will sync all data to disk. Now give it some seconds until you do the next step, to make sure it really wrote everything to disk.
U — will remount all file-systems as read-only. It’s not possible to unmount all file-systems while running, so this is the closest you’ll get.
Now press B which forcefully and immediately reboots.
If all this didn’t work, then either your kernel crashed (at which point your system is already shut down) or this feature is disabled in your current OS/Kernel release. If you want to enable it, have a look at the ArchWiki, it’s a great resource for Linux tutorials even if you don’t use an ArchLinux derivative.
tl; dr
Pressing Alt + SysRq and then very slowly entering “REISUB” while still holding the Alt key will restart your system gracefully. If it doesn’t work, either the kernel crashed or this functionality is disabled.