tobiasmanske.de/content/posts/2018-02-18-sysrq.md
2022-03-23 20:41:41 +01:00

71 lines
2.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Safely reboot a frozen Linux System - SysRq
date: "2018-02-18"
categories:
- misc
tags:
- english
- linux
summary: Sometimes your Linux is just utterly broken and hung up. This is how you make your system shut down in an orderly fashion to avoid data corruption on your file-system
cover:
image: "/images/crashed.jpg"
showtoc: false
---
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 <s>didn't find an answer to the initial
question</s> ([Wikipedia](https://en.wikipedia.org/wiki/Reboot_(computing)#Cold_vs._warm_reboot)
to the rescue) I stumbled upon
[this](https://en.wikipedia.org/wiki/Magic_SysRq_key).
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 "safe" 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.
> **R**eboot **E**ven **I**f **S**ystem **U**tterly **B**roken
**Slowly** pressing those keys together with Alt + SysRq will trigger
the following events.
R &#8212; will take keyboard control away from X.
E &#8212; 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 &#8212; 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 &#8212; 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 &#8212; 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](https://wiki.archlinux.org/index.php/Keyboard_shortcuts#Kernel),
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.