Quick Links

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. Linux provides the tools you’ll need.

Processes and Linux

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.

Remove Ads

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.

The top program running in a terminal window with graphs and color enabled.

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.

Remove Ads

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.

The htop program running in a terminal window with the signal menu displayed, and the highlight on the SIGKILL option.

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
Using the ps command in a terminal window, twice. The second time, the -e option is used.
Remove Ads

You can pipe the output through grep to search for processes by name.

ps -e | grep firefox
Piping the output from ps through grep to search for the firefox process.

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
Using the --forest option with ps to generate a tree diagram of processes, children, and parents.

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.

Remove Ads
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 
Using the -p option with pstree to include PIDs in the tree display.

We’re interested in processes, not threads. Hide threads with the -T (hide threads) option to unclutter the tree.

pstree -p -T
Using the -T option with pstree, to unclutter the tree display by hiding thread processes.

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.

Remove Ads
pstree -pT 14937
Starting the pstree display at an arbitrary process.

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
Using kill to kill a process, and using pstree to verify it has been terminated.

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.

Remove Ads

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
Using pgrep to search for processes whose names contain fox.

In our case, the process called foxglove would also be killed. If we search for “fire” we’d only kill the firefox process.

Remove Ads
pgrep -l fire
Using pgrep to search for processes whose names contain 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
Using pgrep to search for procesess with fire in their name, using pkill to kill them, then verifying with pgrep they have been terminated.

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.

Remove Ads

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
Finding the PID of a process with pgrep, then using renice to adjust its nice value to 15.

Reducing the nice value requires sudo.

pgrep drain
renice 0 18577
sudo renice 0 18577
Using pgrep to obtain the PID of a process, then using renice to reduce its nice value. An attempt without sudo fails, an attempt with sudo succeeds.

The xkill Command: For X Only

The xkill command only works on GNOME, on X. It doesn’t work with Wayland.

Remove Ads

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.

The xkill command running in a terminal window, with the cursor arrow replaced by a skull and crossbones shape.

Moving the cursor to the window of a GNOME application and left-clicking your mouse, terminates that application.

A Powerful Set of Tools

These tools are capable of identifying and killing any process, so be careful!

Make sure the process you’re about to kill isn’t a vital piece of your operating system. If you do, remember that killing a task doesn’t remove it from your computer, so a reboot will fix mistakes. But that’s still an inconvenience, so be certain before you pull the trigger.

Remove Ads