This article covers the basics of Linux commands pgrep
.
pgrep is a command line utility that allows you to find Process ID of the running program based on the given criteria. This can be a full or partial process name, the user running the process, or some other attribute.
ranking pgrep
It is part of the package procps
(or procps-ng
), which comes pre-installed on most Linux distributions.
How to use the pgrep command
The syntax for the pgrep command is as follows:
pgrep [OPTIONS]<PATTERN>
Matching <PATTERN>
Designed with Extended regular expressions.
When called without any options, pgrep
Displays the PIDs of all running programs that match the specified name. For example, to find the PID of your SSH server, you would run:
pgrep ssh
If there are processes running with a name that matches “ssh”, the pid will be shown on the screen. If nothing matches, the result is null or no output is generated.
1039 2257 6850 31279
The command will issue output 0
When at least one operation matches the requested name. Otherwise, the exit code is 1
. This can be useful when used in shell scripts.
If you want to signal an appropriate process, use pkill
. This command is continuous with the pkill command, and uses the same options and pattern matching.
pgrep
print each Process ID Matches a newline. Cucumber -d
Allows you to specify different parameters. For example, if you want to use spaces as delimiters, enter:
pgrep ssh -d' '
1039 2257 6850 31279
Cucumber -l
I tell you pgrep
To show the name of the process along with its id:
pgrep ssh -l
pgrep regular expressions To perform a search and list all processes that have “ssh” in their name:
1039 sshd 2257 ssh-agent 6850 ssh 31279 ssh-agent
If you only want to match processes whose names exactly match the search pattern, you can use:
pgrep '^ssh$' -l
6850 ssh
caret (^
) will watch the game at the beginning of the string, and the dollar $
in the end.
By default, pgrep only matches process names. when the option -f
Use the command matches the entire argument list.
pgrep -f ssh
Use options -u
to know pgrep
To show processes run by a specific user:
pgrep -u root
To select multiple users, separate their names with commas:
pgrep -u root,budi
You can also combine search options and styles. For example to print out all the processes and their names that run under the username “Dear” and have the word “gnome” in their name, you can type:
pgrep -l -u budi gnome
To display only the oldest (oldest) or newest (newest) processes, use the options -n
(for longer) or -o
(latest update).
For example, to find the oldest process started by the “friend” user, you could enter:
pgrep -lnu budi
To reverse the match, for example to only show processes that don’t match the specified criteria, use the option -v
. The following command will print all processes not started by the user “log”:
pgrep -v -u budi
Cucumber -c
I tell you pgrep
Prints only the number of matching runs. For example, to find the processes running as the user ‘budi’, enter the command:
pgrep -c -u budi
conclusion
The pgrep command is used to find out the PID of a running program based on various criteria.
For more information about the pgrep command, visit the pgrep manual page or type man pgrep
at your station.