A Linux system is made up of processes, which are basically programs that run in a system.
However, processes often need to be managed, which can happen in different ways, such as finding, stopping, starting, and killing.
There are times when we don’t know a process name but its process ID or vice versa.
We can use various commands to handle these situations.
Objectives
In this tutorial, we will learn the basics of using two commands, pgkill and pgrep, and how they help us in process management.
Table of Contents
Prerequisites
- Access to a machine running Linux (I’m using Ubuntu 20.04)
- Acting as a non-root sudo user to ensure a secure environment
The pgrep Command
You might be familiar with the grep command, which is used for finding a word or text string from a given piece of text or file.
The pgrep command works in the same manner, but it is designed to work with the processes. It can find the process IDs based on a given condition, for example, a username. We can also provide the process name as a parameter to be searched.
Let’s take an example. We will use the pgrep command with a process name and see all associated process IDs. To do this, we execute the following command:
pgrep bash
In this command, we are passing the process name as the parameter. Let’s see the output, which displays one process ID.
Let’s see what happens when we execute the command with a process name that does not exist. We try this out with the httpd as the process name:
pgrep bash
Notice that no output is returned. This means that this process does not exist.
We will now enter the bash command to run bash once more. It should now have two bash-related process IDs. To run bash, we enter the following command:
bash
After this, we will once again execute the following command to check for the process IDs for bash:
pgrep bash
Notice that the output is displayed on separate lines.
Display Output on a Single Line
We can display the output on a single line by separating each process ID with a blank space. To do this, we need to use the -d parameter. We need to execute the following command:
pgrep bash -d' '
When using the -d parameter, the output is displayed on a single line where a blank space separates each process ID. This is what we had defined as the criteria after the -d parameter.
Display Process ID
What do we need to do if we want to display the process name and its ID in the output? We need to use the -l parameter.
pgrep bash -l
Notice that the output displays bash against each process ID.
Display Process ID for Specific User
We can also display the process IDs of the processes being run by a specific user. For example, we can display the process IDs of the processes for edxd users. We need to use the -u parameter for mentioning the username.
pgrep -u edxd
A complete list of all process IDs is displayed in the output.
We will now use two usernames, edxd and root, and display process IDs. To do this, we need to separate both the usernames with a comma, but we do need to use the -u parameter:
pgrep -u edxd,root
A complete list of all process IDs is displayed in the output.
Display Newest Process
Let’s assume that we want to find the newest process started by the edxd user. To be able to do this, we need to use the -lnu as the parameter:
pgrep -lnu edxd
The output is displayed. The description of the -lnu parameter is as follows:
- -l: can also be used as --list-name. It is used to list PIDs and process names.
- -n: can also be used as --newest, which is used to list the most recent processes
- -u: can also be used as –euid <ID,…>, which matches the given IDs
If you recall, we had started the bash process at the beginning of this article. Therefore, the output displays it as the newest process.
Display Number of Processes
We might even need to count the processes running for a specific user, such as edxd. To do this, we need to execute the following command with the -c parameter:
pgrep -c -u edxd
The -c parameter counts the number of processes. It does not display the list of processes but only the total number of processes for a specific user.
We have seen various parameters of the pgrep command. To get more information about this command, we can run the following command with the --help parameter:
pgrep --help
The output displays a list of parameters with their descriptions. We can mix and match a few parameters to refine our conditions with the pgrep command.
The pkill Command
You may be used to killing a process with the kill command. However, we can use the pkill command to do the same task. The only difference is that the pkill command works only with the command names rather than the process IDs.
We will start a new instance of Firefox from the command line, for demo purposes. To do this, we’ll execute the following command:
firefox
An instance of Firefox is invoked immediately.
We will minimize this and open a new terminal session because the existing terminal session is busy running Firefox.
In the new terminal window, we will execute the pkill command to kill Firefox.
pkill firefox
Notice that no output is returned and the command executed successfully.
Let’s close this terminal window, and we should now be on the first terminal window in which we had launched Firefox. Here, we get a message of AbnormalShutdown and Terminated. This is because we had killed the Firefox process using the pkill command.
Similar to pgrep, you can also get help on pkill using the following command:
pkill --help
Conclusion
Well done. Hopefully, this tutorial helped us understand the pgrep and pkill commands in Linux. If you encountered any issues, please feel free to leave a comment or contact us, and we’ll get back to we as soon as we can.