Pgrep Command in Linux

Introduction

Before we start discussing pgrep command in linux, let's first understand-What is a Pgrep Command ?

The pgrep command in Linux is used to search for processes based on their criteria and return their process IDs (PIDs). It provides a quick and efficient way to identify running processes by specifying attributes such as the process name, user, group, command line, or other criteria. The pgrep command is a useful tool for process management and troubleshooting in Linux systems.

In this tutorial, we have covered the basics of the Linux pgrep.

How to Use the pgrep Command

The pgrep command has the following syntax:

pgrep [OPTIONS] <PATTERN>

Extended regular expressions are used to specify the matching <PATTERN>.

pgrep displays the PIDs of all running programs that match the given name when run without any options. To find the PID of the SSH server, for example, type:

pgrep ssh

The PIDs of all running processes with names that match "ssh" will be displayed on the screen. The output is empty if no matches are found.

Output

1039
2257
6850
31279

When at least one running process has the same name as the one requested, the command returns 0. Otherwise, 1 is the exit code. When building shell scripts, this can come in handy.

Use pkill to deliver signals to the corresponding processes. This command is a wrapper for pkill, and it uses the same arguments and pattern matching as that command.

Each matched process ID is printed on a new line by pgrep. You can use the -d option to specify a different delimiter. If you want to use a space as a delimiter, for example, type:

pgrep ssh -d' '
Output

1039 2257 6850 31279

The -l option instructs pgrep to display the process's name as well as its ID:

pgrep ssh -l

pgrep does the search using regular expressions and returns a list of all processes with the word "ssh" in their names:

Output

1039 sshd
2257 ssh-agent
6850 ssh
31279 ssh-agent

If you only want to match processes whose names match the search pattern exactly, you'd use:

pgrep '^ssh$' -l
Output

6850 ssh
ℹ️
At the start of the string, the caret (^) character matches, and at the end, the dollar $ character matches.

Pgrep matches just against the process name by default. The command matches against the entire argument list when the -f option is provided.

pgrep -f ssh

To tell pgrep to display processes run by a certain user, use the -u option:

pgrep -u root

To specify several users, use commas to separate their names:

pgrep -u root,mark

You can also mix and match search patterns and options. To print all processes and their names that run under user "mark" and have the word "gnome" in their names, type:

pgrep -l -u mark gnome

Use the -n (for newest) or -o (for oldest) options to display only the processes that were started the least recently (oldest) or the most recently (newest).

To identify the most recent process initiated by the user "mark," for example, type:

pgrep -lnu mark

You can also combine the selections without a space between them and with a single dash, as seen in the example above.

Use the -v option to reverse the matching, displaying only processes that do not meet the supplied criteria. All processes that are not being executed by user "mark" will be printed using the following command:

pgrep -v -u mark

The -c option instructs pgrep to just print the number of matching processes. To find the processes that run as user "mark," for example, type:

pgrep -c -u mark

FAQs on Pgrep Command in Linux

How does the pgrep command work?

The pgrep command searches for processes that match a specific criterion specified as an argument. It scans the process table and outputs the PIDs of matching processes.

What are the common criteria used with the pgrep command?

You can use various criteria with the pgrep command, such as the process name (-x option), user/group (-u and -g options), parent process ID (-P option), command line (-f option), and more.

How do I use the pgrep command to search for processes by name?

To search for processes by name using pgrep, simply provide the process name as an argument. For example, pgrep sshd will retrieve the PIDs of all processes with the name "sshd".

Can I use pgrep to search for processes by their command line arguments?

Yes, you can use the -f option with pgrep to search for processes based on their command line arguments. For example, pgrep -f "nginx -c /etc/nginx.conf" will find processes with the specified command line.

How can I search for processes owned by a specific user with pgrep?

Use the -u option followed by the username to search for processes owned by a specific user. For example, pgrep -u username will retrieve the PIDs of processes owned by "username".

Is it possible to negate criteria in pgrep?

Yes, you can use the -v option to invert the matching criteria. For example, pgrep -v sshd will return PIDs of all processes except those with the name "sshd".

How does the pgrep command differ from pidof?

While both pgrep and pidof retrieve process IDs, pgrep offers more flexibility in searching for processes based on various criteria. pidof primarily searches for processes by their executable names and returns a single PID.

Conclusion

The pgrep command is used to determine the PIDs of a running application using various criteria.

Visit the pgrep man page or type man pgrep in your terminal for more information about the pgrep command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.