Pgrep Command in Linux
3 min read
This article covers the basics of the Linux
pgrep is a command-line utility that allows you to find the process IDs of a running program based on given criteria. It can be a full or partial process name, a user running the process, or other attributes.
pgrep command is a part of the
procps-ng) package, which is pre-installed on nearly all Linux distributions.
How to Use the
The syntax for the
pgrep command is as follows:
pgrep [OPTIONS] <PATTERN>
<PATTERN> is specified using extended regular expressions.
When invoked without any option,
pgrep displays the PIDs of all running programs that match with the given name. For example, to find the PID of the SSH server, you would run:
If there are running processes with names matching “ssh”, their PIDs will be displayed on the screen. If no matches are found, the output is empty.
1039 2257 6850 31279
The command returns
0 when at least one running process matches the requested name. Otherwise, the exit code
1. This can be useful when writing shell scripts.
If you want to send signals to the matched processes use
. This command is a wrapper around the
pkill, and uses same options and pattern matching.
pgrep prints each matched process ID on a newline. The
-d option allows you to specify a different delimiter. For example, if you want to use a space as a delimiter, enter:
pgrep ssh -d' '
1039 2257 6850 31279
-l option tells
pgrep to show the process name along with its ID:
pgrep ssh -l
pgrep uses regular expressions to perform the search operation and will list all processes that contain “ssh” in their names:
1039 sshd 2257 ssh-agent 6850 ssh 31279 ssh-agent
If you want to match only the processes which names are exactly as the search pattern, you would use:
pgrep '^ssh$' -l
^) character matches at the beginning of the string, and the dollar
$at the end.
pgrep matches only against the process name. When
-f option is used the command matches against full argument lists.
pgrep -f ssh
-u option to tell
pgrep to display processes being run by a given user :
pgrep -u root
To specify multiple users, separate their names with commas:
pgrep -u root,mark
You can also combine options and search patterns. For example to print all processes and their names that run under user “mark” and contains “gnome” in their names you would type:
pgrep -l -u mark gnome
To display only the least recently (oldest) or the most recently (newest) started processes, use the
-n (for newest) or the
-o (for oldest) option.
For example, to find the newest process started by the user “mark”, you would enter:
pgrep -lnu mark
As you can see from the example above, you can also combine the options without a space between them and with a single dash.
To reverse the matching, i.e. to show only processes that do not match the given criteria, use the
-v option. The following command will print all processes that are not being run by user “mark”:
pgrep -v -u mark
-c option tells
pgrep to print only the count of the matching processes. For example to find the processes that run as user “mark”, enter:
pgrep -c -u mark
pgrep command is used to find out the PIDs of a running program based on different criteria.
For more information about
pgrep command, visit the pgrep man
page or type
man pgrep in your terminal.
If you have any questions or feedback, feel free to leave a comment.