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 curl Command: Make URL Requests and Inspect HTTP Responses

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Learn how to make URL Requests and Inspect HTTP Responses with the Linux curl command, including practical examples, key options, and important precautions.

Linux head Command: Display the Beginning of a File

Linux head Command: Display the Beginning of a File

Learn how Linux head prints the first lines or bytes of files and pipelines, labels multiple inputs, excludes a trailing portion, and handles multibyte data.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.

Linux tar Command: Create and Extract Archives

Linux tar Command: Create and Extract Archives

Learn how to create and Extract Archives with the Linux tar command, including practical examples, key options, and important precautions.

Linux truncate Command: Shrink or Extend File Size

Linux truncate Command: Shrink or Extend File Size

Learn how to shrink or extend files with Linux truncate, adjust sizes relatively, match a reference file, and distinguish sparse logical size from disk usage.

Linux last Command: Review Login and Reboot History

Linux last Command: Review Login and Reboot History

Learn how to use the Linux last command to review user login sessions, system reboots, and shutdown history, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Linux Tutorial / Navigate Directories with pwd, ls, cd, and tree

Use pwd, ls, cd, and tree to find your current location, inspect directories, navigate paths, and check where each command takes you.

Linux awk Command: Analyze Text by Fields

Linux awk Command: Analyze Text by Fields

Learn how to analyze Text by Fields with the Linux awk command, including practical examples, key options, and important precautions.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Compare find and locate for file searches, then use which, whereis, and Bash type to distinguish executable paths from shell command resolution.