Linux systems are known for their stable operation, designed to be operational without reboot for years. However if you still manage to pull a string and freeze up your computer there are quite a few ways to get the system back to work. You can login to a virtual console, use pgrep and pkill, use xkill, system monitor and kill the offending process if you are sure about them. Or if nothing works you can hit the power button may be? But before you head towards that power button there is one last magical way to get back your system, and I am going to talk about it in this article.
First off I am sure there would be many of us who don’t know that their keyboard has a sysrq key. Is it? Or perhaps you had forgotten about it? Yes its that PrintScreen/SysRq key that we are going to MakeUseOf in this article.
What is a Magic SysRq Key
A Magic SysRq Key lets you send some low level commands independent of the system state. This means that if the system is frozen you can use these key combinations to perform certain actions and thus regain control.
So what’s the magic? Well if you are ever stuck with an unresponsive system try
Pheew! So how many hands do you need to do this? Or should you use your toes? Well, actually you only press and hold Alt and PrintScreen together, the other keys are pressed one at a time and then released. There is quite a lot happening when you do this. ‘r’ ’s’ ‘e’ ‘i’ ‘u’ ‘b’ actually stand for seperate commands:
- r – takes the control of the keyboard back from X.
- s – writes the data from the disc cache to the hard disk.
- e – sends SIGTERM to all processes except init.
- i – sends SIGKILL to all processes except init
- u – remounts all the filesystems readonly (basically a measure to help you reboot safely)
- b – reboots the system
As you see that pressing each letter accomplishes certain tasks, so it makes sense to press them one by one while having the Alt+PrintScreen combination depressed. Give sufficient time before pressing the next key to allow the system to perform all the tasks as requested by the Magic SysRq Key combination
Other Magic Commands
That’s not all. Alt+PrintScreen+r+s+e+i+u+b is just one special combination of the commands that helps you recover an unresponsive system. There are other commands available as well:
0 – 9
sets the console log level, controlling which kernel messages will be printed to your console so that you don’t get flooded.
restarts the system without making steps to ensure that the conditions are good for a safe reboot, using this key alone is like doing a cold reboot.
sends SIGTERM to all processes except init.
call Out Of Memory Killer, which will kill a process that is consuming all available memory.
displays help about the SysRq keys on a terminal though in actuality you can use any key except for the ones specified, to display help.
sends SIGKILL to all processes except init.
kills all processes on the current terminal.
sends SIGKILL to all processes, including init.
dumps memory info to your console.
shuts down the system via ACPI or in older systems, APM.
dumps the current registers and flags to your console.
dumps all timers info to your console.
takes keyboard and mouse control from the X server.
writes all data from the disc cache to the hard-discs, it is a sync and is necessary to reduce the chances of data corruption.
dumps a list of current tasks and info to your console.
remounts all mounted filesystems read-only. After using this key, you can reboot the system with Alt+SysRq+B without harming the system.
dumps uninterruptable (blocked) state tasks.
Alt+Printscreen is also the keyboard shortcut for screenshot of the active window under Gnome and some other window managers.Make sure you have Magic SysRq Keys enabled on your system. Magic SysRq keys/commands work only if the kernel was compiled with the CONFIG_MAGIC_SYSREQ option.
You can also use proc sysrq trigger calls to control the behavior of sysrq keys. Generally you only need to know that
echo 0 > /proc/sys/kernel/sysrq disables sysrq keys
echo 1 > /proc/sys/kernel/sysrq enables sysrq keys
There are other numbers with special meanings like 2 – enable control of console logging level, 4 – enable control of keyboard (SAK, unraw), 8 – enable debugging dumps of processes etc, 16 – enable sync command, 32 – enable remount read-only, 64 – enable signaling of processes (term, kill, oom-kill), 128 – allow reboot/poweroff, 256 – allow nicing of all RT tasks(control the nice level(priority) of Real Time tasks)
Alternatively adding kernel.sysrq=1 in /etc/sysctl.conf file will also enable sysrq keys.
Please be extra careful while editing configuration files.
There you have it. Your very own contingency plan. If you find the combination difficult to memorize you may write it down and paste it on monitor, or just remember “Raising Skinny Elephants Is Utterly Boring”.
Magic SysRq keys are nothing new, they have been a feature of the kernel since quite some time now. Have you ever got the chance to use them? or do you prefer some other way? Share with us in the comments.