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.

More in This Category
Linux fdisk Command: Inspect and Edit Disk Partitions

Linux fdisk Command: Inspect and Edit Disk Partitions

Learn how to inspect and Edit Disk Partitions with the Linux fdisk command, including practical examples, key options, and important precautions.

Linux bzip2 Command: Compress a File in bzip2 Format

Linux bzip2 Command: Compress a File in bzip2 Format

Learn how to compress a File in bzip2 Format with the Linux bzip2 command, including practical examples, key options, and important precautions.

Linux reboot Command: Restart the System Safely

Linux reboot Command: Restart the System Safely

Learn how to restart the System Safely with the Linux reboot command, including practical examples, key options, and important precautions.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux dd Command: Copy and Convert Data by Blocks

Linux dd Command: Copy and Convert Data by Blocks

Learn how to copy and convert files or block devices with Linux dd, choose block sizes and ranges, display progress, verify results, and avoid overwriting the wrong device.

Linux whoami Command: Show the Effective User Name

Linux whoami Command: Show the Effective User Name

Learn how to show the Effective User Name with the Linux whoami command, including practical examples, key options, and important precautions.

Linux info Command: Browse GNU Documentation

Linux info Command: Browse GNU Documentation

Learn how to use the Linux info command to browse hierarchical GNU manuals and navigate Info nodes, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.

Linux touch Command: Create Files and Change Timestamps

Linux touch Command: Create Files and Change Timestamps

Learn how to use the Linux touch command to create empty files and change access or modification timestamps, including reference times, date formats, and symlink handling.

Linux nice Command: Set a New Process's Scheduling Priority

Linux nice Command: Set a New Process's Scheduling Priority

Learn how to set a New Process's Scheduling Priority with the Linux nice command, including practical examples, key options, and important precautions.