Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

A frozen desktop can be really infuriating, especially when we first start using Linux on the desktop. Many beginners will try to hard reboot the entire machine, which might lead to data loss.

In this tutorial, we’ll experiment with different solutions and workarounds to safely resolve this issue. Some of these solutions are X11 specific, but a few others will work on Wayland as well.

2. Solutions

We’ll start by asking the kernel nicely to terminate the processes causing the issue. For instance, the desktop session might be working fine, but one of the windows might not be responding to interactions.

Afterward, for halted desktop sessions, we’ll try to terminate the session and restart it again. Finally, if all these methods fail, we’ll pull out the big guns to forcefully kill the processes causing the issue or reboot the entire machine.

2.1. Terminating the Processes

For processes that are dead slow or not responding, we can simply open up our terminal to kill these specific processes. Of course, we can also use a task manager like GNOME System Monitor for the same purpose.

On Linux, we have a lot of ways to terminate a process, one of which is to simply use the kill command:

$ kill 2722

The kill command will send a SIGTERM signal by default. A SIGTERM signal asks the process to save and clean up its data and quit itself.

However, this might not always work, and we may have to force kill the process. In that case, we can send another signal called SIGKILL (-9) that will force kill the process:

$ kill -SIGKILL 2722
$ kill -9 2722

It seems a bit tedious to look for the PID of the process and then kill it. Therefore, we can use the other kill commands like pkill to kill a process that matches a pattern:

$ pkill firefox*

Or we can use killall to kill all instances of a certain process:

$ killall firefox

Yet another option is to use pgrep to grep the PID of a process and kill it:

$ kill -9 $(pgrep firefox)

Mind that we should avoid using sending SIGKILL if the process is doing some critical job like reading or writing data.

For the most part, these basic commands should be enough for us to deal with frozen windows. However, there’s a much easier and faster way to kill graphical processes, which we’re going to cover in the next section.

2.2. xkill

The xkill command only works for the X Windows System (X11). The tiny utility forces the X Server to terminate its connection with the specified client window, which can be any running graphical process.

Additionally, xkill doesn’t ship with the X11 packages by default. However, it should be available in our distro’s official package repository under the name xorg-xkill. We can install it using a package manager like apt or yum.

Once we have xkill, we can simply launch it with the xkill command:

$ xkill

Once we run the command, we’ll notice that our cursor changes to a skeleton or cross emblem. At this point, we can click on any window that we want to terminate:

xkill Command

Once we click on a window, the window will be closed, and the xkill command will quit.

We should know that when we have a bunch of child windows that belong to a parent window, then killing one of these windows will terminate all of the windows (including the parent window).

Moreover, we can make this process faster by binding a shortcut key like <SUPER>+X to the xkill command. This process will be slightly different for different distributions, but usually, we bind keys in the Shortcuts tab of our Keyboard Settings.

2.3. Virtual Terminal (TTY)

We can switch to a TTY terminal when we can’t access our regular interactive terminal. TTY is a virtual terminal that we can run without using graphical environments on our desktop machines. Therefore, if our graphical desktop is frozen, TTY will, most of the time, run without issues.

Normally, most Linux distributions provide us with up to nine virtual terminals. Each virtual terminal runs in its own process. For that reason, we can easily switch between multiple virtual terminals for multitasking.

On most major distributions like Ubuntu and Fedora, we can access the virtual terminal by pressing <ALT>+<CTRL>+<FN KEY>. For instance, pressing <ALT>+<CTRL>+F1 will launch TTY 1 for us.

Once we launch a TTY, it will prompt us for login. After successfully logging in, we have access to a fully functional full-screen terminal:

Virtual Terminal

At this point, we can either execute the kill commands or use a TUI task manager like top or htop to gracefully terminate the processes that are causing the issues.

Moreover, once we are done, we can switch back to our desktop session with <ALT>+<CTRL>+F7. The 7th TTY is where, most of the time, a desktop session lies. However, we should note that our desktop session might be running on a different TTY. In that case, we should switch between the other TTYs to look for our desktop session.

2.4. Terminating the Desktop Session

Sometimes, our desktop session is frozen, and we don’t have any choice but to terminate and reload the entire session. For some desktop environments, there might be a reload hotkey or a command that we can use.

For instance, on SwayWM (Wayland), we have a default binding that reloads the desktop session. On KDE-based desktop environments, we can press <CTRL>+<ALT>+<ESCAPE> to refresh the KDE session.

Nonetheless, if the desktop environments don’t have this option, we can kill the session process. If we’re running X11, we can simply kill the session from a TTY:

$ pkill x

For Wayland-based desktop environments, we can simply kill a process that specifically manages the session. For instance, on Ubuntu, we can send a SIGQUIT signal to gnome-shell:

$ kill -3 gnome-shell

Or if we’re running dbus, we can simply restart it:

$ dbus-run-session -- gnome-shell --nested --wayland

Killing the desktop session will most likely take us to a virtual terminal or the login manager screen where we can re-login to our desktop session.

2.5. Final Resort: Raise the Elephant

As our final resort, when our machine is stuck and won’t respond to any keystrokes, we can try out some of these Magic SysRq shortcuts in sequence before hard rebooting. These key sequences are specifically handled by the Linux kernel regardless of the system state. While handling these key sequences, the Kernel tries to recover the machine from a frozen state without corrupting the filesystem.

These key sequences involve a magic SysRq key, which will vary from keyboard to keyboard. On laptops, it can be invoked with the <Fn> key, while on regular keyboards, it can be invoked with the <PrntScr> key:

  • <ALT>+<SysRq>+R — switches keyboard to “raw” mode
  • <ALT>+<SysRq>+E — sends SIGTERM to all processes except init
  • <ALT>+<SysRq>+I — sends SIGKILL to terminate the remaining processes
  • <ALT>+<SysRq>+S — syncs all filesystems to prevent data loss
  • <ALT>+<SysRq>+U — remounts all filesystems as read-only
  • <ALT>+<SysRq>+B — reboots the system

If this method still doesn’t work, then we should hard reboot our box as there’s no other choice.

3. Conclusion

In this article, we went through different methods to deal with frozen graphical windows and desktop sessions. We started by going through some simple approaches and worked our way up to more aggressive methods to unfreeze our Linux machine.

Finally, we went through the Magic SysRq key sequences to safely reboot our machine without data loss.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!