close
close

Association-anemone

Bite-sized brilliance in every update

How to manage Linux processes using ps, kill and pkill
asane

How to manage Linux processes using ps, kill and pkill

Everything running on your Linux computer is a process. Processes should play nice with others, but sometimes they need to be taken in hand. Linux gives you the tools you need.

Processes and Linux

Linux computers use a special set of programs and temporary file systems to boot the computer to the point where the operating system can be started. The nucleus then creates the first user space process, by launching the systemd process.

All processes are assigned 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 the regular user space processes running on your computer are descendants of systemd.

If a process is poorly written or has run into an error condition, it may freeze and stop responding or display other undesirable behavior. If this is the case, you will have to reign in the process or even kill it.

These commands and utilities provide a comprehensive toolset for managing processes.

Top command: a dynamic system dashboard

The above command gives you a dynamic view of processes run on your computer. Each process is displayed as a row in a table, with column entries representing various statistics about that process. The table is resized as the values ​​change.

Top program running in a terminal window with graphics and color enabled.

Top program running in a terminal window with graphics 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 allows you to quickly identify the 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 want to kill. When prompted for the signal, type 9 then press enter.

htop command: Another dynamic system dashboard

The htop program is top notch, made a little friendlier. You can have colors and graphics on top, but htops graphics are more configurable and the use of color is a design feature and not an afterthought. The columns are the same as above unless you configure them differently.

The htop program runs in a terminal window with the signal menu displayed and the SIGKILL option highlighted.

The htop program runs in a terminal window with the signal menu displayed and the SIGKILL option highlighted.

To kill a process in htop, highlight the process, press lowercase k (or F9), move the menu highlight to SIGKILL, and press Enter.

The ps command: lists processes from the command line

Order ps list processes in a terminal windowand display their PID. By default, you are only shown processes that you have launched. To see all processes, use the -e (all) option.

ps
ps -e
Using the ps command in a terminal window, twice. The second time, the -e option is used.

Using the ps command in a terminal window, twice. The second time, the -e option is used.

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

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

Passing the output from ps through grep to search for the Firefox process.

The –forest option formats the output into a tree, making it easier to identify child and parent processes. This is useful because if you kill a parent, you also kill all the children.

ps -e 
Using the --forest option with ps to generate a tree diagram of processes, children, and parents.

Using the –forest option with ps to generate a tree diagram of processes, children, and parents.

Order pstree: a more attractive tree

The pstree command generates process trees that show parent-child relationships in a structured, compact, and informative way.

Multiple identical children are displayed as a single entry with the names of the child processes enclosed in parentheses.

parent-5*(child-processes)

The number and asterisk * show how many child processes there are. Parentheses indicate that processes are threads. In this case, the display name is the name of the parent process.

parent-3*({name-of-parent})

To display PIDs, use the -p (display PIDs) option.

pstree -p 
Using the -p option with pstree to include the PIDs in the tree display.

Using the -p option with pstree to include the PIDs in the tree display.

We were 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 remove the tree display by hiding thread processes.

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

You can start the tree on any process by providing its PID. Here, PIDs were requested, no strings and to start the tree with PID 14937.

pstree -pT 14937
Starting the pstree display to an arbitrary process.

Starting the pstree display to an arbitrary process.

Kill order: You’re done

The kill command terminates the process a with a given PID. If you don’t own the process, you’ll need to use sudo.

In the previous example, we have a terminal window running the Bash shell, which has launched a process called drain.

Well pass the drain PID to kill and turn it off.

kill15197
pstree -pT 14937
Using kill to kill a process and using pstree to check that it has finished.

Using kill to kill a process and using pstree to check that it has finished.

The drain process has been stopped.

pgrep command: Names will be taken

You have to find the PID of a process before killing it, it works fine, but it’s a little weird. The pkill command skips a step by accepting a process name instead of the PID. However, all matching processes are killed.

If the lookup hint happens to occur in another process name, that other process will also be stopped. For example, using dbus as a search hint kills the dbus process and also the dbus-daemon process.

The pgrep command tells you which processes match your search indexbut leaves the matching processes untouched. You can use it to run the pkill command.

Let’s say you want to kill Firefox. What would be killed if you used the fox as a search clue? The -l (namelists) option tells pgrep to include the process name in the output.

pgrep -l fox
Using pgrep to search for processes whose names contain fox.

Using pgrep to search for processes whose names contain fox.

In our case, the process called foxglove would also be killed. If we’re looking for fire, we’ll just kill the Firefox process.

pgrep -l fire
Using pgrep to search for processes whose names contain fire.

Using pgrep to search for processes whose names contain fire.

pkill and killll command: List of hits

Both pkill and llall accept a search hint and kill the matching processes. The difference between them is that Killall doesn’t try to do partial matches like pkill and pgrep, it only works with whole names.

pgrep -l fire
pkill fire
pgrep -l fire
Using pgrep to search for processes with fire on their behalf, using pkill to kill them, then checking with pgrep that they have been terminated.

Using pgrep to search for processes with fire on their behalf, using pkill to kill them, then checking with pgrep that they have been terminated.

I checked the processes with threads in their name and found two examples of firefox. I killed them with pkill and verified they were gone.

Renice command: Social integration

As I said earlier, processes should play nice with others. CPU time is finite and must 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 HIGH the number, the more beautiful the process. Allows processes with lower numbers to be served before them.

The below the number, the more self-centered a process is. She wants to be dealt with before further trials. The average process is launched with a nice value of zero, which means treat me like everyone else.

Renice order adjusts the pleasant value of a process. Set a good value of 15 for 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.

Finding the PID of a process with pgrep, then using renice to adjust its nice value to 15.

Reducing the beautiful value requires sudo.

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

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

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.

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

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

Moving the cursor into a GNOME application window and left-clicking the mouse closes that application.

A powerful set of tools

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

Make sure that the process you are about to kill is not a vital part of your operating system. If you do, remember that killing a task doesn’t remove it from your computer, so a restart will fix the bugs. But this is still an inconvenience, so be sure before you pull the trigger.