Linux pgrep Command: Find Process IDs by Name or Criteria
pgrep finds the process ID of running processes based on conditions such as process name and user. It is a command to check the target PID and does not terminate the process by itself.
What is the pgrep command?
The basic pattern is applied to the process name using a regular expression. Use -x if the entire name must match exactly, and -f to check the full command line. -f can match more broadly, so be sure to verify the results.
Basic syntax
pgrep [option] pattern
Installed implementations and options may vary depending on the distribution. Check the description on the current system with man pgrep.
Examples
Search PID by name
Outputs matching PIDs line by line.
pgrep sshd
View name and PID together
Displays which tasks are selected to reduce mistakes.
pgrep -a sshd
Exact name restriction with user
Narrow the scope to avoid selecting processes with the same name of other users.
pgrep -u alice -x python3
Reason why search results show as PID
pgrep outputs the PID of processes that match the conditions. Using -a shows both the PID and the full command line, making it easier to check.
pgrep -a sshd
Example output (on a system where the process is running):
1234 /usr/sbin/sshd -D
If no lines appear, there may be no matching process, or there could be permission or condition issues. By default, the search targets the process name, and to search the full command line, use -f. Broad patterns may include unexpected processes, so check the results before passing them to termination commands.
Main Options and Format
| Options/Format | Description |
|---|---|
-a |
Display PID along with the full command line. |
-x |
The entire process name must match the pattern. |
-f |
Check the full command line instead of the process name. |
-u user |
Restrict the scope to valid user IDs. |
-n / -o |
Select either the most recent or the oldest match. |
Precautions when using
The process name may differ from the visible command line, and long names may be affected by the internal limits of the implementation. -f may find more jobs than expected. Before passing the found PID to kill, double-check with ps -p PID -o cmd= or similar.
Frequently Asked Questions
Is it an error if there are no results from pgrep?
It usually means that there are no processes matching the condition and the exit status may not be 0. To confirm the service status, also check the execution time and search conditions.
Official Documentation
You can check the exact behavior of the options and implementation differences in the official pgrep documentation.









