Linux pgrep command
pgrep is a very useful process-finding tool in Linux systems; it is part of the procps or procps-ng package. The command's name comes from "process grep"; as its name suggests, it can search for processes in the system just as the grep command searches text.
The main function of pgrep is to find running processes based on given criteria and return their PIDs (process IDs). Compared with the traditional approach of combining the ps command with grep, pgrep is more concise and efficient.
pgrep basic syntax
The basic syntax of the pgrep command is as follows:
pgrep [选项] [匹配模式]
Where:
选项: various options for specifying search criteria or output format匹配模式: a regular expression pattern for matching process names
Commonly used options
pgrep provides many options to precisely control search behavior; the following are commonly used options:
| Option | Description |
|---|---|
-l |
Display the process name and PID together |
-a |
Display the full command line rather than just the process name |
-f |
Match the full command line (including arguments) rather than just the process name |
-u |
Match only processes owned by a specified user |
-x |
Match the entire process name exactly |
-n |
Display only the newest (most recently started) matching process |
-o |
Display only the oldest (earliest started) matching process |
-c |
Return only the number of matching processes without displaying PIDs |
-d |
Specify the output delimiter (newline by default) |
-P |
Match only child processes of a specified parent process ID |
-g |
Match only processes in a specified process group ID |
-t |
Match only processes on a specified terminal (tty) |
Usage examples
Basic process lookup
Find all processes named "nginx":
pgrep nginx
This command returns the IDs of all processes whose names contain "nginx".
Display the process name
Find "ssh" processes and display the process names:
pgrep -l ssh
Example output:
1234 sshd
5678 ssh-agent
Match the process name exactly
Match only processes named exactly "bash" (not "bashrc", etc.):
pgrep -x bash
Match the full command line
Find processes containing specific arguments, for example, find sshd processes using a specific port:
pgrep -f "sshd -p 2222"
Filter by user
Find all processes run by the user "www-data":
pgrep -u www-data
Combine conditions for lookup
Find processes named "mysqld" run by the user "mysql":
pgrep -u mysql mysqld
Count processes
Count the number of running "chrome" processes:
pgrep -c chrome
Find the newest/oldest process
Find the most recently started "python" process:
pgrep -n python
Find the earliest started "python" process:
pgrep -o python
Advanced usage
Use with pkill
pgrep is often used together with the pkill command: you can first use pgrep to see which processes will be terminated, confirm, and then use pkill:
Example
First view the matching processes
pgrep
-l
apache2
After confirming, terminate these processes
pkill apache2
Use a custom delimiter
By default, pgrep uses newlines to separate multiple PIDs. You can use a comma instead:
pgrep -d, nginx
Example output:
1234,5678,9012
Find processes on a specific terminal
Find all processes running on the tty1 terminal:
pgrep -t tty1
Find child processes
Find all child processes of the process with PID 1234:
pgrep -P 1234
Frequently Asked Questions
What is the difference between pgrep and ps | grep?
- Efficiency: pgrep queries kernel process information directly, making it more efficient than ps | grep
- Safety: pgrep does not match its own process, whereas ps | grep may match the grep process itself
- Function: pgrep provides more specialized process search options
Why does pgrep sometimes fail to find a process I know is running?
Possible reasons:
- The process name is misspelled
- Did not use
-fAn option matches the full command line - The process belongs to another user and you do not have sufficient permission to view it
- The process has already terminated
How do I check the version of pgrep?
pgrep -V
Practical Application Scenarios
Monitor service status
Check whether a service is running in a script:
Example
if
pgrep
-x
"nginx"
&
gt;
/
dev
/
null;
then
echo
"Nginx is running"
else
echo
"Nginx is not running"
fi
Batch process operations
Get the PIDs of all Java processes and perform operations:
Example
for
pid
in
$
(
pgrep
java
)
;
do
echo
"Processing Java process $pid"
Other operations...
done
System resource monitoring
Count the number and resource usage of a specific type of process:
Example
count
=$
(
pgrep
-c
chrome
)
memory
=$
(
ps
-o
rss
=
-p
$
(
pgrep chrome
)
|
awk
'{sum+=$1} END {print sum}'
)
echo
"Chrome processes: $count, Total memory: ${memory}KB"
Summary
pgrep is a powerful and efficient tool in Linux system administration; it simplifies process lookup and management. By mastering pgrep's various options and usage, you can:
- Quickly and accurately locate specific processes
- Write more reliable system administration scripts
- Improve the efficiency of daily system maintenance work
Remember, after using pgrep to find processes—especially when you are about to terminate them—be sure to confirm that you have found the correct processes, so as to avoid accidentally terminating important services.
Linux Command Reference