JMM’s notes on

The Magic SysRq key

“Oh no not again”

Recently I encountered an out-of-memory (OOM) situation that I needed to get out of. I couldn’t remember the right Magic SysRq incantation, but I thought “It’s probably Alt+SysRq+o where the ‘o’ stands for ‘OOM’”.

Nope.

The “o” stood for “off”. I turned my computer off during an upgrade. D’oh! Thankfully it’s NixOS, so files weren’t being upgraded in-place. However, some downloads were interrupted so they got corrupted, and now I’m rescanning my Nix store.

Anyway, here’s a cheat sheet for my future self. I assume I’m browsing this article on my phone because my laptop is frozen.

Sequences I tend to use

This is the order I tend to go in. Each key is prefixed with “Alt-SysRq-”.

  1. r — Turns off keyboard raw mode. Sometimes this is enough to let me switch to another console with Control+Alt+F1 (if you’re not on a graphical console you don’t have to press Control, just Alt+Fn where n is the number of a console).
  2. v — Restores framebuffer console? This might get you the console, actually. I think this might have helped when I had a weirdly frozen Wayland compositor.
  3. Depends if I want to try to kill everything on the console already. If so, press Alt+SysRq+k (see step 5).
  4. f — Call OOM killer. Each press might only kill one process. Sometimes I wait a bit and press Alt+SysRq+f a few more times. Often this is enough to be able to regain control.
  5. k — Try to kill all programs on current console.
  6. If this doesn’t work, I’ll basically try to kill the rest of the processes:
  7. e — Send SIGTERM to all processes except init. Wait for a bit.
  8. i — Send SIGKILL to all processes except init.
  9. s — Sync filesystems.
  10. u — Attempts to remount systems as read-only. I just cargo-cult this step because of the mnemonic “raising elephants is so utterly boring”. I don’t think I’ve been in a situation where I really understood or appreciated what this does.
  11. b — Reboot.

And here are some other handy keys:

h
(or any other key not defined)
Prints a help message to the console. Hopefully you’re at a console.
d
Shows locks that are held?
t
Current tasks and information.

Documentation

kernel.org has a good article on the SysRq key. Actually, it’s part of the docs (Here’s the same page, but in .txt form).

Here’s a direct link to the table of command keys.

SysRq key location

For laptops without a SysRq, you can press the PrtSc key. I forgot how I found that out. I probably found this out through Kernel.org’s article. Also, while you’d normally need to press Fn-PrtSc to effect a SysRq, you don’t need to press Fn when doing Alt-SysRq-whatever.