How to Use the pgrep and pkill Commands in Linux to Find and Terminate Processes by Name

Managing runaway background services, hanging web servers, or zombie processes in Linux historically required a clunky two-stage pipeline: running ps aux | grep <process_name> to identify the Process ID (PID), followed by typing kill <PID> to send an exit signal. In multi-user or high-density container environments, this manual approach is slow and prone to typographical errors.

The pgrep and pkill utilities—part of the core Linux procps-ng package—eliminate this friction. By matching processes directly by executable name, user ownership, terminal, or command-line arguments, they allow administrators to query and signal processes safely in a single command. In this guide, we examine syntax, essential filtering flags, signal handling, and safe operational practices.

How pgrep and pkill Differ from ps and kill

Understanding how pgrep and pkill improve upon classic command combinations highlights their utility in automation scripts:

Command Primary Function Output Format Self-Matching Protection
ps | grep Dumps full process table through a text pipe filter Full terminal string output No (Often catches the grep process itself)
pgrep Scans /proc filesystem directly for matching process names Clean list of PIDs (one per line) Yes (Never matches itself)
pkill Matches processes using pgrep logic and immediately sends a signal Silent by default; sends POSIX signal Yes (Protects administrator shell)

When troubleshooting network-bound daemons discovered with pgrep, pairing process management with the Netcat command in Linux allows you to test listening port reachability before terminating services.

Using pgrep to Find Process IDs

To locate the PID of any running service, provide the name of the executable:

pgrep nginx

Output returns pure numerical PIDs, making it ideal for bash scripts:

1420
1421
1422

Displaying Process Names Alongside PIDs (-l)

To verify which specific processes are returned before issuing kill commands, pass the -l flag:

pgrep -l python

Matching Full Command Lines (-f)

By default, pgrep only evaluates the binary name (up to 15 characters). If you are running multiple Python or Java scripts and want to match a specific script name, pass -f to search the full argument string:

pgrep -fl "backup_database.py"

Filtering by User or Group (-u)

To list processes belonging to a specific system user:

pgrep -u www-data

Using pkill to Terminate Processes Safely

Once you verify your pattern using pgrep, replace it with pkill to send a signal directly to matching processes:

pkill nginx

By default, pkill dispatches SIGTERM (signal 15), requesting that applications save state and terminate gracefully.

Sending Forceful Termination Signals (-9)

If an application is unresponsive or deadlocked in memory, issue a forceful SIGKILL signal:

pkill -9 -f "stuck_worker.py"

Reloading Services with SIGHUP (-HUP)

Many system daemons (such as Nginx, Apache, or sshd) reread their configuration files upon receiving a hangup signal without terminating active client connections:

sudo pkill -HUP nginx

Safe Usage Recommendations to Avoid Accidental Outages

Because pkill executes matches without prompting for confirmation, keep these safety precautions in mind:

  • Always test with pgrep first: Run pgrep -fl <pattern> before running pkill <pattern> to inspect every PID and process title that matches your filter.
  • Use exact matching (-x): Running pkill sh will accidentally kill bash, sshd, and other essential system shells. Use pkill -x sh to enforce strict, exact-name matching.
  • Isolate by User: Running pkill -u alice python ensures you do not inadvertently terminate Python jobs belonging to other developers on a shared server.

For ongoing system diagnostic workflows, pairing process debugging with the strace command in Linux reveals internal system calls made by failing processes before you terminate them.

Frequently Asked Questions

What exit codes do pgrep and pkill return?

Both utilities return exit code 0 if one or more matching processes were found, 1 if no matching processes were detected, and 2 or higher for syntax and permission errors. This makes them reliable in shell conditional statements.

Can regular users kill processes owned by root or other users?

No. Standard POSIX permissions apply. A non-privileged user can only signal their own user-level processes. Terminating daemons owned by root or system service accounts requires sudo.

What is the difference between pkill and killall?

killall matches strictly on the exact process name. pkill is more versatile, supporting regular expressions, full command-line matching (-f), user filtering (-u), and terminal filtering (-t).

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.