Hung Linux System? How to Escape to the Command Line and More

It is not much fun when your Desktop hangs. The fear of lost work, the inability to continue work, and more. But it need not always be like this. Knowing just a little extra – a few shortcut keyboard combinations and a few commands at the command line – will hopefully get you back up and running quickly. It does not always work, but it works often.

In this tutorial, you will learn:

  • How to do basic immediate troubleshooting steps on a hung system
  • How to execute a memory test using a Memtest86+ ISO image burned to disc
  • A number of keyboard shortcuts that will empower you to handle hung systems better
  • Information about what avenues to take to do further troubleshooting steps

Hung Linux System? How to Escape to the Command Line and More

Hung Linux System? How to Escape to the Command Line and More

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux Distribution-independent
Software Bash command line, Linux based system
Other Any utility which is not included in the Bash shell by default can be installed using sudo apt-get install utility-name (or yum install for RedHat based systems)
Conventions # – requires linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires linux-commands to be executed as a regular non-privileged user

A truly hung system?

One of the main difficulties with system hangs is finding out what is causing the hung. It is a bit of an art, or perhaps better a skill, which will grow over time. The more system hangs you see, the more you will recognize when they happen again. If you see many, it will be easy to analyze computers from others, even with limited information like during remote phone support.

If the complete system hangs (press the NUM LOCK and/or CAPS LOCK keys a few times; does the lights still come on the keyboard indicating their turned on/off? Similarly, move the mouse around on the screen; is it stuck?



If the answer to both questions is “no change,” the system is highly likely completely frozen. However, you could still try – on a Linux system – CTRL+ALT+F4 (more on this later) and CTRL+ALT+BACKSPACE (On some Linux systems this will reset the desktop/GUI completely) to see if it makes any difference.

On Windows, you could try CTRL+ALT+DEL which will hopefully bring you back to a Windows shortcut screen from where you can select Task Manager to start investigating if there is any hung process, etc. The remainder of this article will focus on Linux only.

Note that the CTRL+ALT+DEL sequence may also work on some Linux systems, so try it too.

If the system is genuinely hung 100%, completely frozen, and responds to nothing, there is but one solution left; press the RESET button on the computer case, or turn it off completely and then restart. After a restart, you can start looking at the logs (for example dmesg may show issues with a disk on reboot) to see if anything looks amiss, though it is somewhat likely that whatever made the system hung did not write output to the log before the hung/system freeze occurred.

To learn more about dmesg, please see our ‘What Is Dmesg and How Do I Use It‘ article!

In cases like these, you will want to download a Memtest86+ ISO CD Image (or any similar tool) and burn it to CD, then start your system from the burned disk and start the memory testing once Memtest86+ comes up (which should be automatically the case with their ISO images). Let it do a solid test on your system’s memory.

Full system freezes/hangs are not normal and potentially caused by faulty hardware. Memory and disk are prime suspects. As noted above, watch dmesg for disk (and potentially memory) issues during startup, and check your memory in full with a tool like Memtest86+. Note that you can use such a tool even if the operating system you are running is not Linux but, for example, Microsoft (or even MacOS).

The operating system never needs to (and will not) be loaded if you select the burned CD as the startup drive. A small Linux operating system will be loaded (it is part of the ISO image), after which Memtest86+ will be started from the same CD.

Note also that may need to press a key during startup, like F9 or some other function or another keyboard key, to select the startup disk to be the CD. You may also need to enter your BIOS to select the startup disk as the CD and/or to enable booting from CD in the first place.

For the disc, make sure to use the correct burning method; you do not want to write the ISO file to a disk; you will want to use a ISO-enabled burning software and write the ISO to disc in an expanded format. Think of the ISO as a zip file that contains the files that actually need to be burned. Popular Linux burning programs like K3b have an option similar to More actions… (button in the main menu in K3b) Burn Image… that allow you to select an ISO to be burned.

Finally, your CPU, GPU, or other peripherals may be faulty. I would always approach the CPU towards the end of the troubleshooting chain unless it was improperly cooled or overclocked. The GPU is a more likely cause, especially if it is a higher-end card and/or was overclocked or poorly cooled. You can start seeing where the art, or rather the learning process comes into play.



Less hung than frozen?

If your system is still somewhat responsive, unlike our example above, and reacts to, for example, mouse movements -or- reacts to any keyboard shortcut sequence -and/or- the keyboard lights still turn on/off when you press any of the LOCK keys as described above, then potentially more troubleshooting steps can be made.

For Linux users, the first thing to always try is to press the following keystrokes in sequence. Stop and explore when you notice that something is working.

ALT+TAB (this keyboard sequence will change the active window you’re working on it. Does it work? If so, can you click on another window once at least one window swap was done with ALT+TAB?) > ESC (We are trying to escape whatever state a program is in.

It is unlikely to help, but we want to make sure) > CTRL+c (This one will be especially in hung or busy shell sessions and will interrupt whatever program is running) > CTRL+z (You can think about this one as a strong version of CTRL+c, though what it really does is place a process into the background and pauses it, it is again mostly if not only applicable to the shell).

If any of this set your system free you can keep exploring to see if anything (and what in particular) makes the system hung again. If you want to terminate any process interrupted with CTRL+z in the terminal/shell, you can execute kill %1, provided that the output generated by the CTRL+z stated [1] (in other words, match the numbers: 1-1, 2 if 2 etc. and leave the literal percentage sign).

Frozen, differently

If your system is responding to some keys (for example ALT+TAB) but is still unusable, or any such similar situation of some response, but still unusable you will want to try escaping/dropping to the command line.

Doing so will allow you to execute for example ps -ef | grep ‘likely_failing_process_name and then sudo kill -9 mypid where mypid is the PID (process identifier) given in the second column (the first numerical column that is) in front of the likely hung process. In other words, usually, a single process is responsible for making the system semi-hung. Primary suspects are usually Firefox and Chrome, and LibreOffice Writer or Calc after this.

If it is a server, other major programs like mysqld may be causing the problem. It is not recommended to kill -9 such server software instances without doing further troubleshooting with knowledge of the server process being used, how it is deployed, what fault tolerance there is etc. But for most other programs, you should be fine as per above, though note you may lose unsaved work in those processes.

That said, most programs these days have some level of crash recovery that can be enabled beforehand (when the program is functioning normally). It is a good idea to do so now in preparation for a potential crash later.



Dropping to the command line

Once we want to drop to the command line, simply press CTRL+ALT+F4. Note that F4 here stands for the F4 function key in the row of function keys at the top of your keyboard. Note also that we could have also used for example F3 or F5, though not for example F7.

The reason for this is that – whilst there are various terminals available – the F7 terminal (and sometimes F2 from memory) are reserved for the graphical user interface, the Desktop. Thus, you could, for example, start by swapping to the terminal using CTRL+ALT+F4 and then swap back to the Desktop (though it may have already failed to swap to the terminal to start with!) by using CTRL+ALT+F7.

If this did not clear the hang, login to the terminal session in the CTRL+ALT+F4 screen (or similar), and also have a look at CTRL+ALT+F1 which may display a system log. From within the authenticated terminal session you can use ps -ef etc. to kill deemed rogue processes etc., as described in the last section.

Once you are in the command line, you may like to have a look at the init command (by using man init, and referencing this answer on AskUbuntu). For example, running sudo init 3 followed by sudo init 5 could restart the desktop environment (your work will be lost though).

You may also find this init command interesting for example installing NVIDIA GPU drivers which sometimes require one to stop the X server before installing them. To do so, you can use sudo init 1, then browse to the directory of the extracted drives and initiate ./nvidia-install and follow the prompts provided. This may not always work if there are prior driver issues, but it does provide another opportunity to explore when issues arise.

Last resort

If all else fails, and the system seems semi-hung, but nothing has helped, start randomly moving the palms of your hands over many keys, randomly over and across the keyboard, pressing many keys in the process. Various keyboard combinations may potentially trigger a different state in some program or in the operating system, and this can sometimes unlock the system.

Conclusion

This article explored basic troubleshooting steps for hung/frozen systems in various degrees of being frozen. We discussed how to execute a memory test using a CD with Memtest86+ burned from an ISO image. We also looked at a number of keyboard shortcuts that will empower you to handle hung systems better, and we discussed what avenues to take to do further troubleshooting steps for previously hung systems.

I will recant from the idea to end this article with my usual enjoy!, as a hung system is no joy. Still, this article may help you a good stretch on the right way. I hope your [next] debugging session goes well!



Comments and Discussions
Linux Forum