Situatie
Everything running on your Linux computer is a process. Processes ought to play nicely with others, but sometimes they need to be taken in hand.
Solutie
Linux computers use a special set of programs and temporary file systems to boot your computer to the point where the operating system can be started. The kernel then creates the first user space process, by launching the systemd process.
All processes are given a number as a process identifier, or PID. The systemd process has a PID of one. Processes can launch other processes, known as child processes. The process that launched them is called the parent process. All regular user space processes running on your computer are descendants of systemd.
If a process is poorly written or has entered an error condition, it might freeze and become unresponsive, or display some other unwanted behavior. If that’s the case, you’ll need to reign in that process, or even kill it. These commands and utilities provide a comprehensive toolkit for managing processes.
The top Command: A Dynamic System Dashboard
The top command gives you a dynamic view of the processes running on your computer. Each process is shown as a line in a table, with column entries representing different statistics about that process. The table is re-sorted as values change.
Ctrl+Shift+M sorts by the %MEM column, and Shift+P sorts by the %CPU column. Sorting a column twice reverses the sort order. This lets you quickly spot processes that are using the most RAM or CPU time.
To kill a process, press lowercase “k” then type the PID of the process you’re going to kill. When you’re prompted for the signal, enter 9 then press enter.
The htop Command: Another Dynamic System Dashboard
The htop program is top, made slightly friendlier. You can have colors and graphs in top, but htop’s graphs are more configurable, and the use of color is a design feature and not an afterthought. The columns are the same as in top, unless you configure them differently.
To kill a process in htop, highlight the process, press lowercase “k” (or F9), move the menu highlight to the SIGKILL, and press Enter.
The ps Command: List Processes From the Command Line
The ps command lists processes in a terminal window, and displays their PID. By default you’re shown only processes you’ve launched. To see all processes, use the -e (everything) option.
ps
ps -e
You can pipe the output through grep to search for processes by name.
ps -e | grep firefox
The –forest option formats the output into a tree, making it easy to identify child and parent processes. This is useful because if you kill a parent, you kill all of its children, too.
ps -e –forest
The pstree Command: A More Attractive Tree
The pstree command generates process trees showing parent and child relationships in a structured, compact, and informative way.
Multiple identical children are shown as a single entry with the name of the child processes enclosed in brackets.
parent-5*[child-processes]
The number and asterisk “*” show how many child processes there are. Curly brackets indicate the processes are threads. In which case, the name shown is the name of the parent process.
parent-3*[{name-of-parent}]
To display PIDs, use the -p (show PIDs) option.
pstree -p
We’re interested in processes, not threads. Hide threads with the -T (hide threads) option to unclutter the tree.
pstree -p -T
You can start the tree at any process, by providing its PID. Here, we’re asking for PIDs, no threads, and to start the tree with PID 14937.
pstree -pT 14937
The kill Command: you’re Terminated
The kill command terminates the a process with a particular PID. If you don’t own the process, you’ll need to use sudo. In the previous example, we’ve got a terminal window running the Bash shell, which has launched a process called drain.
We’ll pass the PID of drain to kill, and terminate it.
kill 15197
pstree -pT 14937
The drain process has been killed.
The pgrep Command: Names Will Be Taken
Having to find the PID of a process before you kill it works just fine, but it is a little clunky. The pkill command skips a step by accepting a process name instead of a PID. However, all matching processes are killed.
If your search clue happens to appear in another process name, that other process will also be killed. For example, using “dbus” as your search clue kills the dbus process, and the dbus-daemon process, too. The pgrep command tells you what processes match your search clue, but it leaves the matching processes untouched. You can use it to dry-run your pkill command.
Let’s say you want to kill firefox. What would get killed if you used “fox” as the search clue? The -l (list names) option tells pgrep to include the process name in the output.
pgrep -l fox
In our case, the process called foxglove would also be killed. If we search for “fire” we’d only kill the firefox process.
pgrep -l fire
The pkill and killall Command: The Hit List
Both pkill and killall accept a search clue and kill matching processes. The difference between them is, killall doesn’t try to do partial matches like pkill and pgrep, it works with whole names only.
pgrep -l fire
pkill fire
pgrep -l fire
We checked for processes with “fire” in their names, and found two instances of firefox. We killed them with pkill, and checked they were gone.
The renice Command: Social Integration
As I said earlier, processes ought to play nicely with others. CPU time is finite, and needs to be shared. But, some processes are critical, and deserve more CPU time than lower-priority tasks.
All processes have a nice value, between -19 and 20. The higher the number, the nicer the process is. It’s letting processes with lower nice numbers be serviced ahead of itself.
The lower the number, the more self-centered a process is. It wants to be treated ahead of other processes. The average process is launched with a nice value of zero, which means “treat me like everybody else.” The renice command adjusts the nice value of a process. We’ll set a nice value of 15 on the drain process.
pgrep drain
renice 15 18577
Reducing the nice value requires sudo.
pgrep drain
renice 0 18577
sudo renice 0 18577
The xkill Command: For X Only
The xkill command only works on GNOME, on X. It doesn’t work with Wayland.
If you’re running GNOME on X, typing xkill in a terminal window changes the cursor to an “x” or other symbol such as a skull and crossbones. The shape depends on your distribution. Moving the cursor to the window of a GNOME application and left-clicking your mouse, terminates that application.