Check whether a program is running
-x requires the whole name to match: without it pgrep nginx would also find
nginx-exporter.
The pgrep command prints the PIDs of processes by name or command line, owner and parent — no ps or grep needed.
pgrep nginx
PIDs of processes with nginx in the name
pgrep -l nginx
PID and name
pgrep -a nginx
PID and the full command line
pgrep -x sshd
the whole name, not a part
pgrep -f 'app.py --dev'
by the whole command line
pgrep -u www-data -l
a user's processes
pgrep -c -x nginx
how many there are
pgrep -n python3
the newest; -o the oldest
pgrep -P 1234 -l
children of process 1234
pidof cron
PIDs by exact program name
pgrep finds processes and prints their PIDs, one per line. The pattern is a regular
expression, and by default it's matched against the process name, that is, the
program name:
The kernel stores a process name of at most 15 characters: systemd-journald shows up
as systemd-journal. The full command line with path and arguments is available with
-f (match it) and -a (print it).
pgrep never lists itself — unlike ps aux | grep, where grep's own line ends up in
the result. Exit status: 0 — found, 1 — nothing, 2 — bad options. Script checks
are built on it.
Selection options (-u, -P, -t, -x) combine with AND. Full list — man pgrep.
| Option | What it does | Example |
|---|---|---|
-l --list-name |
Print the PID and the process name. | pgrep -l nginx |
-a --list-full |
Print the PID and the full command line. | pgrep -a nginx |
-f --full |
Match the pattern against the whole command line, not just the name. | pgrep -f 'app.py --dev' |
-x --exact |
The name (or the line with -f) must match exactly. | pgrep -x sshd |
-u --euid user |
Only this user's processes (effective UID). | pgrep -u www-data -l |
-c --count |
Print only the number of matching processes. | pgrep -c -x nginx |
-n --newest |
Only the newest match; -o the oldest. | pgrep -n python3 |
-P --parent PPID |
Only children of the given parent. | pgrep -P 1234 -l |
-i --ignore-case |
Case-insensitive matching. | pgrep -i -l Xorg |
-d --delimiter string |
Separator between PIDs instead of a newline. | pgrep -d, -x nginx |
Nothing found. Try a shorter query.
-x requires the whole name to match: without it pgrep nginx would also find
nginx-exporter.
All Python scripts share the process name python3; they differ by command line. So
scripts are found with -f, and ^ at the start keeps it from matching another
command line that merely mentions the path in its arguments.
-d, joins the PIDs with commas — the form ps -p accepts, see ps.
pkill takes the same selection options as pgrep but sends a signal — see kill.
The process name is cut to 15 characters, so a longer pattern matches nothing. Shorten
it to the first 15 characters or match the command line: pgrep -f systemd-journald.
The pattern turned up in someone else's command line — for example, a bash -c shell
that itself runs pgrep -f …. Anchor the pattern to the start: pgrep -f
'^/usr/bin/python3 /opt/app/worker.py' — or match the name with -x.
Nothing matched; the exit status is 1. Check the letter case (or add -i), the program
name in ps -e, and whether it's truncated. pgrep sees other users' processes without
sudo.
There's no user with that name. Check id name, or give the UID as a number.
Found an inaccuracy? Report an error in the article