Pgrep Command in Linux | mstvlife
This article covers the basics of the Linux
pgrep is an order line utility that permits you to discover the procedure IDs of a running project dependent on given criteria. It very well may be a full or halfway procedure name, a client running the procedure, or different properties.
pgrep command is a part of the
procps-ng) package, which is pre-installed on nearly all Linux distributions.
How to Use the
pgrep Command #
The syntax for the
pgrep command is as follows:
pgrep [OPTIONS] <PATTERN>
<PATTERN> is specified using extended regular expressions.
When conjured with no alternative, pgrep shows the PIDs of every single running project that coordinate with the given name. For instance, to discover the PID of the SSH server, you would run:
In the event that there are running procedures with names coordinating “ssh”, their PIDs will be shown on the screen. On the off chance that no matches are discovered, the yield is vacant.
1039 2257 6850 31279
The order returns 0 when at any rate one running procedure coordinates the mentioned name. Something else, the leave code is 1. This can be valuable when utilized in shell contents.
If you want to send signals to the matched processes use pkill. This command is a wrapper around the pkill, and uses same options and pattern matching.
pgrep prints each ID pairing process on a new line. The -d option allows you to specify a different delimiter. For example, whether to use a delimiter space, 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 utilizes customary articulations to play out the hunt activity and will list all procedures that contain “ssh” in their names:
1039 sshd 2257 ssh-agent 6850 ssh 31279 ssh-agent
In the event that you need to coordinate just the procedures which names are actually as the inquiry design, you would utilize:
The caret (
^) 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 likewise consolidate choices and search designs. For instance to print all procedures and their names that run under client “mark” and contains “elf” 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 turn around the coordinating, for example to show just procedures that don’t coordinate the given criteria, utilize the – v alternative. The accompanying order will print all procedures that are not being controlled by client “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
The pgrep order is utilized to discover the PIDs of a running project dependent on various 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.