Where to start First steps track · 15 stages
Linux Labs

pgrep — find a process PID by name

5 min Ubuntu 24.04 · procps-ng 4.0.4

The pgrep command prints the PIDs of processes by name or command line, owner and parent — no ps or grep needed.

Cheat sheet

10 commands Printable version
  • 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

Overview

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:

text
student@ubuntu:~$ pgrep -l systemd
1 systemd
322 systemd-journal
386 systemd-udevd
student@ubuntu:~$ pgrep -a cron
729 /usr/sbin/cron -f -P

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.

Options

10

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.

Recipes

4

Check whether a program is running

bash
if pgrep -x nginx >/dev/null; then echo running; else echo no; fi

-x requires the whole name to match: without it pgrep nginx would also find nginx-exporter.

Find the script, not the interpreter

bash
pgrep -l python3                        # every python3 at once
pgrep -af '^/usr/bin/python3 /opt/app/worker.py'   # the script you need

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.

Look at the details of what you found

bash
ps -o pid,ppid,user,stat,etime,args -p "$(pgrep -d, -x nginx)"

-d, joins the PIDs with commas — the form ps -p accepts, see ps.

List before pkill

bash
pgrep -a -u student sleep     # who will be hit
pkill -u student sleep        # the same conditions, now a signal

pkill takes the same selection options as pgrep but sends a signal — see kill.

Troubleshooting

4

pattern that searches for process name longer than 15 characters

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.

pgrep -f finds an extra process

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.

pgrep prints nothing

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.

pgrep: invalid user name

There's no user with that name. Check id name, or give the UID as a number.

See also

  • ps — details on processes: columns, state, parent.
  • kill — send a signal to the process you found, pkill.
  • top — which of the found processes loads the system.
  • processes — what a process, its name and command line are.
  • regex — the pgrep pattern is a regular expression.
Loading…