Skip to content

pgrep Command Cheat Sheet

pgrep looks through the currently running processes and lists the process IDs (PIDs) which match the selection criteria. It is safer and easier than ps aux | grep.


Synopsis

pgrep [options] pattern

Basic Usage

Find PID by Name

pgrep sshd
# Output:
# 1050
# 1234

List Name and PID (-l)

Shows the process name alongside the ID.

pgrep -l sshd

List Full Command Line (-a)

Shows the full startup command.

pgrep -a python
# Output:
# 3455 python manage.py runserver

Advanced Filtering

Full Command Search (-f)

By default, pgrep only matches the process name (first 15 chars). Use -f to match against the full argument list.

# Find the PID of the python script "my_worker.py"
pgrep -f "my_worker.py"

By User (-u)

Find processes owned by root.

pgrep -u root sshd

Inverse Match (-v)

Find processes NOT matching criteria.

pgrep -v systemd

Count Matches (-c)

Don't show PIDs, just count how many are running.

pgrep -c nginx

Using with Other Commands

pgrep output is perfect for piping.

# Top for specific processes
top -p $(pgrep -d',' nginx)
(-d',': Delimit PIDs with comma).


Notes

  • pkill: pgrep finds them; pkill kills them. They share most flags.
  • Exit Code: 0 (One or more matches), 1 (No matches), 2 (Syntax error).