pgrep — Find PIDs of running processes by name — the lookup half of pkill across all 5 shells
Equivalents in every shell
pgrep firefoxCompanion to `pkill`. Prints one PID per line — perfect for `for pid in $(pgrep firefox); do ...`. `-a` adds the command line beside the PID; `-l` adds just the process name; `-f` matches the full command line (same as `pkill -f`); `-d ,` joins PIDs with commas — handy for `kill -9 $(pgrep -d , firefox)`.
pgrep firefoxSame external. macOS BSD `pgrep` is mostly compatible — `-a` (cmdline) and `-l` (name) both work; `-d` (delimiter) is supported. `-c` (count only) is the same on both. For the oldest of cross-platform scripts, the lowest-common-denominator is `pgrep -f "<pattern>"` without `-a` or `-l`.
pgrep firefoxSame external. Fish-specific idiom for "kill if running": `set pid (pgrep firefox); and kill $pid`. Avoid `$(pgrep ...)` — fish prefers `(pgrep ...)` for command substitution. Exit status: 0 if ≥1 match, 1 if no match, 2 on error.
Get-Process -Name firefoxNo wildcard-supporting `pgrep` analog ships as a cmdlet, but `Get-Process` is the canonical equivalent. Wildcards: `Get-Process firefox*`. Full-cmdline grep: `Get-CimInstance Win32_Process -Filter "CommandLine LIKE '%pattern%'" | Select-Object ProcessId, CommandLine`. For JSON: `Get-Process firefox | Select Id, ProcessName | ConvertTo-Json`.
tasklist /FI "IMAGENAME eq firefox.exe"`/FI` accepts the filter format `<column> <op> <value>`. Columns: `IMAGENAME`, `PID`, `STATUS`, `USERNAME`, `WINDOWTITLE`, `MEMUSAGE`. Operators: `eq`, `ne`, `gt`, `ge`, `lt`, `le`. To get JUST the PIDs: `tasklist /FI "IMAGENAME eq firefox.exe" /FO CSV /NH` then strip-and-parse the CSV.
Worked examples
List PIDs matching a name
pgrep firefox(Get-Process firefox).Idtasklist /FI "IMAGENAME eq firefox.exe" /FO CSV /NHShow PID + command line for matches
pgrep -a firefoxGet-CimInstance Win32_Process | Where-Object Name -eq firefox.exe | Select ProcessId, CommandLinewmic process where "name='firefox.exe'" get processid,commandlineTest if a process is running (boolean exit code)
pgrep -q firefox && echo "running"if (Get-Process firefox -ErrorAction SilentlyContinue) { "running" }tasklist /FI "IMAGENAME eq firefox.exe" | findstr /I firefox.exeGotchas
- Same 15-char COMM truncation issue as `pkill` on Linux. `pgrep chromium-browser` returns nothing; `pgrep -x chromium-browse` works; `pgrep -f chromium-browser` is the most reliable. The truncation is a kernel-level limit on `/proc/<pid>/comm`, not a `pgrep` bug.
- `pgrep` without flags matches as a SUBSTRING regex — `pgrep node` matches both `node` and `nodemon`. To require an exact word match: `pgrep -x node`. To match an anchored regex: `pgrep "^node$"` (works because the argument is treated as a regex by default).
- PowerShell `Get-Process -Name foo` THROWS a terminating error if no processes match — unlike `pgrep` which returns exit 1 silently. Always pair with `-ErrorAction SilentlyContinue` in scripts: `Get-Process firefox -ErrorAction SilentlyContinue`. Or check via `Get-Process | Where-Object Name -eq firefox` which returns empty array on no match.
- On Windows, `tasklist` requires the `.exe` suffix in `IMAGENAME` filters. `tasklist /FI "IMAGENAME eq firefox"` returns NOTHING; `tasklist /FI "IMAGENAME eq firefox.exe"` works. PowerShell `Get-Process firefox` (no `.exe`) is the consistent cross-platform spelling.
- Both `pgrep -f` and `Win32_Process.CommandLine` lookups can race with very short-lived processes — a script that does `pgrep firefox && pkill firefox` may see the process between the two calls. For atomicity, `pkill firefox` itself (or `Stop-Process -Name firefox`) does the lookup and kill in one syscall flow.
WSL & PowerShell Core notes
Common tasks using pgrep
- Count the running processes
Report how many processes are alive — useful for load testing, capacity baselining, or detecting fork-bomb / runaway-parallelism conditions.
- Find a process ID by name
Look up the PID(s) of a running process given its executable name — the lookup that precedes most kill / signal / inspect operations.
- List zombie processes
Find defunct child processes whose parent forgot to `wait()` for them — the classic "Z" state — for triaging fork-bomb-like leaks, badly-written supervisors, and PID-table exhaustion incidents.