Overview
`pgrep` filters processes by various conditions such as process name, user ID, terminal, and parent process ID, returning the corresponding PIDs. It is useful for finding and controlling processes or checking their status in scripts.
Key Features
- Supports regular expression-based searching
- Filters processes by user/group
- Allows searching by full command line
- Provides options to select the oldest or newest process
Key Options
Search Criteria
Output Format and Sorting
Generated command:
Try combining the commands.
Description:
`pgrep` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Find PID of a Specific Process
pgrep sshd
Finds the process ID for the `sshd` process.
Find Process PIDs by User
pgrep -u root
Finds the IDs of all processes run by the `root` user.
Output PID and Name Together
pgrep -l firefox
Outputs the ID and name of the `firefox` process together.
Search by Full Command Line
pgrep -f "apache2.*worker"
Finds the IDs of processes whose full command line includes both `apache2` and `worker`.
Find Exactly Matching Process
pgrep -x ssh
Finds the process ID for processes named exactly `ssh`. (`sshd` will be excluded).
Find the Newest Process
pgrep -n chrome
Finds the PID of the most recently started `chrome` process.
Tips & Precautions
`pgrep` is powerful, but misuse can lead to unintended process termination or impact, so caution is advised.
Integration with pkill
Instead of manually passing PIDs found by `pgrep` to the `kill` command, using the `pkill` command allows you to directly terminate or send signals to processes using the same search criteria as `pgrep`, which is convenient. For example, `pkill -f "nginx.*worker"` directly terminates `nginx` worker processes.
Utilizing Regular Expressions
`pgrep` supports regular expressions by default. This allows for complex pattern matching.
- `pgrep 'httpd|nginx'` finds `httpd` or `nginx` processes.
- Using `-f` option with regular expressions like `pgrep -f '^/usr/bin/python3.*script.py$'` can precisely find specific script executions.
Using the -x Option for Exact Matching
`pgrep` performs partial matching by default. For instance, `pgrep ssh` will find not only `ssh` but also `sshd`. To find processes named exactly `ssh`, use the `-x` option. This is important to prevent unintended impact on other processes.