Find the PID (Process ID Number) and Kill the Process, using the Terminal
Linux users do not like to admit it, but, there are occasions when you get stuck in a window, or a desktop and there seems to be no way out, short of hitting the reset button.
The problem with that is that all your unsaved work goes away, along with the offending program.
Guide Overview
The purpose of this tutorial is to show you how to recover from a frozen desktop, without having to reboot.
I will be showing you four different options, each with it's own features, to find the PID of a running process using the terminal command line interface.
CAUTION: Randomly killing processes can cause your system to become unstable. Follow the instructions. Do NOT guess at your PID numbers.
Tools Needed:
A working Linux system. This tutorial should not be specific to any one distribution.
Preparation:
When a Desktop has become unresponsive, our first choice is to open the "Task Manager" in the menu, under "System". However, if your screen is completely frozen, then you will not be able to do that.
That does not necessarily mean that your computer is completely locked up. Most users who are familiar with "Terminal" emulators know the keyboard combination to open a Terminal window.
For the novice, all you have to do is press the following three keys simultaneously:
<ctl>+<alt>+<t>
This will open a terminal window open on your desktop. However, that terminal window is still presented as part of the GUI desktop. As such, it too can be unresponsive.
Things are not always lost, just because we can't see them.
You can open a true terminal, outside the X11 windows system, and enter your commands through it.
The keystroke combination to switch from GUI to terminal is:
<ctl>+<alt>+<f1>
Pressing these three keys together will put your GUI desktop into the bacground, and bring up the TTY (TeleType) interface. So called because back in the day, I acutally accessed the server using an IBM Selectric TeleType Writer.
You simply log in with your user name and password, and you will have full privileges of the system, but without the GUI.
To return to your Desktop, use this combination:
<ctl>+<alt>+<f7>
You don't have to log our of the terminal. You can switch back and forth, between the two.
Try it!
Step How to find the PID of a running process by name.
Once you have an active terminal, you can use several commands to solve your issue.
Lets' look at the simplest one first. Do you know the name of the process that you want to terminate? If so, then a simple command will give us it's PID.
For our purpose, I am going to use the Firefox browser.
The command is:
pgrep firefox
This command will return a numeric string. That number is the PID, or Process ID Number of Firefox.
Using the "kill" command to terminate a process.
To terminate the program, enter the "kill" command, using the PID:
sudo kill -9 nnnnnn
NOTE: You have to be the "Super User" or system administrator in order to kill a process.
You have to provide your user name and password, before it will do so.
Why -9?
The switch, -9 tells the kill command to terminate. It tells the kill command to send the "SIGKILL" signal to the process. This is the fastest way to kill a GUI window.
Substitute the actual PID number of the process you want to kill for "nnnnnn"
If we go back to our desktop, <ctl>+<alt>+<f7>, the Firefox window will be gone, not just suspended.
Step If you don't know the process name.
If you don't know the process name you can manually search for it.
What name the process has for a title in the menu, may not be the exact name the the system knows it by.
For example, the program gnome-disks appears in the menu under the name "Disks". When I use pgrep disks, it comes back with more than one PID, of which only one is actually the program I want to kill. So how do I know which is which?
To list all running processes.
The command to list all running processes is:
ps -aux
The -a and x switches lift restrictions, to show all running processes.
The -u switch says to list the users.
Another way to enter the command is to use the "| less" pipe.
ps -aux | less
You will get a print out to the screen, listed page by page.
Pressing "enter" will scroll through, one line at a time.
Pressing "pgdn" will scroll through, one page at a time.
Simply scroll down until you find the program you want to kill, and note its' PID.
The advantage to this is that you get a frozen snapshot of all running processes.
In the GUI Task Manager, the list constantly changes as programs do things in the background.
Step Is there something more "Human Readable" that I can use?
Fortuanately we have a more human readable way to find PID's.
Almost all newer distributions of Linux now come with a program called "top" installed as default.
Simply enter the command:
top
and you will see a text based version of your GUI task manager.
It behaves the same as the GUI Task Manager, in that as memory usage changes, so does the listing.
To exit from top, just type "q".
The best for the last.
I recommend that if you are more command line oriented, that you should install a program called "htop".
htop is a full text based program, that offers f-key functions, search, filter, kill, and others.
To install the repository version of htop simply use:
sudo apt update
sudo apt install htop
It looks best in a full window, or the TTY screen.
Cheers!
Naught
Edited by Mike_Walsh, 07 June 2021 - 07:37 PM.