pgrep
Processes and system
Usage
pgrep [-f] <pattern>Prints just the PIDs a pattern matches, using exactly the same rules as pkill, which makes it the dry run to do before you kill anything.
Common flags
| Flag | Meaning |
|---|---|
| -f | Match the full command line, so arguments count too. |
| -a | Print the command line next to the PID (Linux). |
| -l | Print the process name next to the PID. |
| -x | Exact name match only. |
| -u <user> | Restrict to one user. |
| -c | Print how many matched instead of the PIDs. |
| -n / -o | Newest / oldest match only. |
Examples
pgrep -af nodeEvery node process with its full command line.
pgrep -x sshdThe PID of sshd itself, not of anything merely mentioning ssh.
kill $(pgrep -f "celery worker")Stop the processes the pattern found.
What is running and what is eating the machine. Identifying it comes before killing it.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does pgrep do?
Prints just the PIDs a pattern matches, using exactly the same rules as pkill, which makes it the dry run to do before you kill anything.
Q. How do I type it?
pgrep [-f] <pattern> — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
7 are listed here; the full set is in man pgrep. This command sits under Processes and system.
Related commands
man page: man pgrep