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-”.
- 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).
- 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.
- Depends if I want to try to kill everything on the console already. If so, press Alt+SysRq+k (see step 5).
- 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.
- k — Try to kill all programs on current console.
- If this doesn’t work, I’ll basically try to kill the rest of the processes:
- e — Send
SIGTERM
to all processes except init. Wait for a bit. - i — Send
SIGKILL
to all processes except init. - s — Sync filesystems.
- 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.
- 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).
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.