The fundamental reason behind a system hang or freeze is typically resource exhaustion; resources necessary for some part of the system to run are not available, due to being in use by other processes or simply insufficient. It’s very common problem to a programmer’s life. Even a database query responding with large data set can eat up so much memory that a system can freeze or an infinite loop is capable enough to stall the system anytime.
Eventually in this kind of situations if the system is not rebooted without taking proper care then that may leads to corrupt filesystem also. One of our DevOps guys taught me how to take care if the system hangs.
The magic “SysRq” key is a key combination understood by the Linux kernel directly. It allows the user to trigger low-level commands regardless of the system’s state. It’s often used to recover from freezes or to reboot a system without corrupting the filesystem. Its effect is similar to the computer’s hardware reset button but with many more options and much more control.
This key combination provides access to disaster recovery and prevents filesystem corruption. The key combination consists of Alt, SysRq and another key, which controls the command issued. The subsequent starts by keeping the Alt+SysRq key pressed (The SysRq key is usually the print screen key). In case of a freeze where system is hanged or not responding to anything- press:
Important: (Alt+SysRq) needs to be pressed for the entire sequence.
Each of the keys after Alt+SysRq is a command. It’s executed immediately when pressed. So, a small period of time should be given to each commands so that their action can be carried out properly e.g. hold the R key for 1-2 seconds, before moving on to E. If the sequence doesn’t work at first, then increase the time period between each sequence key press and try again.
A good way of remembering the sequence R+E+I+S+U+B, just remember Raising Elephants Is So Utterly Boring or Reboot Even If System Utterly Broken.
Raw – Takes control of keyboard back from X
tErminate – Does a “kill SIGTERM” on programs, allowing then to terminate gracefully.
kIll – Does a “kill SIGKILL” on unterminated programs.
Sync – Flushes data to disk.
Unmount – Remounts the filesystem as read-only.
reBoot – Reboots the machine.
If interested check Wikipedia, explains the entire process with much more details.