Luke Oliff.

TIL: Managing Processes With pgrep and pkill

·TIL·1 min read·Luke Oliff

Finding and killing processes by name is a muscle memory skill. pgrep finds them, pkill sends the signal.

pgrep -f "node server.js"

Returns the PID of any process matching the pattern. The -f flag matches against the full command line, not just the process name.

pkill -f "node server.js"

Kills every matching process. Equivalent to pgrep piped to xargs kill.

pkill -9 -f "stuck-process"

The -9 flag sends SIGKILL instead of SIGTERM. Use it when a process ignores the polite shutdown request.

pgrep -a -f "ffmpeg"

The -a flag shows the full command line alongside the PID. Useful when you have multiple ffmpeg processes running with different arguments.

Is this safe in scripts?

Be precise with the pattern. pkill -f “node” kills every Node process on the system. Match a specific filename or unique argument.