pgrep Command Examples in Linux

The Process Grep, commonly known as pgrep, is another popular method used for finding process IDs at the shell. If we know the process name, then we can specify it with the pgrep command. The pgrep command displays the PID of processes that match any given pattern, similar to how grep is used to match patterns in a text file. Patterns can include: the name or user ID (UID) of the user who invoked it; the start time; the parent PID; and more.

You can use pgrep to help you identify a process based on multiple factors when you don’t know its exact PID. Identifying the PID is typically the first step in managing the process.

Syntax

The syntax of the pgrep command is:

# pgrep [options] {pattern}

pgrep Command Examples

1. Return PIDs of any running processes with a matching command string:

# pgrep process_name

2. Search for processes including their command-line options:

# pgrep --full "process_name parameter"

3. Search for processes run by a specific user:

# pgrep --euid root process_name

4. To list processes with the sshd command name, which is also owned by root, execute the command line as follows:

# pgrep -u root sshd

5. To list processes owned by the user root or daemon, execute the command line as follows:

# pgrep -u root,daemon

6. The pgrep command can be used to list processes by specifying a pattern containing the process name. The following command line will find the PID of process that has the name of Firefox:

# pgrep firefox

7. The following example shows the usage of the pgrep command to list all the processes of the user student:

# pgrep -U student

8. The -d option specifies an output delimiter other than the default new line:

# pgrep COMMAND -d DELIMITER_STRING
# pgrep bash -d ":"
1255:1680

9. The -c option returns the count of matching processes:

# pgrep -c COMMAND
Related Post