pkill — Terminate processes by name
💬 "pkill command"
pkill finds processes by name pattern and sends a signal. It reduces the hassle of combining ps+kill.
To avoid accidentally killing other processes, it is important to use -f and get into the habit of checking targets first.
- Concept: Sends a signal to targets based on process name/command-line patterns.
- Default signal: If not specified, requests graceful termination with SIGTERM(15).
- -f: Searches the entire command line.
- -0: Only checks targets without actually killing them.
Commonly used options
| Option | Description |
|---|---|
| -f | Finds the pattern in the entire command line (without -f, only the process name is used) |
| -u <user> | Targets only processes of a specific user |
| -9 | Forces termination with SIGKILL (when graceful termination fails) |
| -0 | Only checks targets without sending a signal |
3 essential command combinations
pgrep -af 'batch.sh'— Check targets first (safest)pkill -u myapp -f 'queue:work'— Terminate by specifying both user and patternpkill -TERM -f 'php-fpm' && sleep 2 && pgrep -af php-fpm— Check for leftover processes after graceful termination
💬 "Show me how to use this as a monitoring metric"
Here are common situations on live production servers, organized step by step.
Step 1 · Check targets
First, use pgrep to see what will be killed.
pgrep -af 'queue:work'
Step 2 · Request graceful termination
Request termination with SIGTERM.
pkill -TERM -f 'queue:work'
Step 3 · Check for leftover processes
If any remain, investigate the cause and consider forced termination.
pgrep -af 'queue:work' || echo CLEAN
How it is used in practice
1. Clean up stuck workers
Situation — Queue workers are piling up unresponsive, so you need to launch new workers.
pkill -TERM -f 'queue:work' && sleep 2 && pgrep -af 'queue:work'
Result — Confirm that the workers terminated gracefully and no processes remain.
Point — Send TERM first so in-flight work finishes safely.
2. Only processes for a specific user
Situation — On a shared server used by multiple users, clean up only your own tasks.
pkill -u deploy -f 'deploy.sh'
Result — Only deploy user processes are terminated, so other users are unaffected.
Point — Without -u, processes with the same name belonging to other users will also be killed.
Things to watch out for
- If you provide a short name pattern without -f, the wrong processes may match.
- -9 skips cleanup work and carries a risk of data loss.
- If you mistakenly terminate critical system processes (such as sshd), your connection will drop.
- Always check targets with pgrep -af before executing.
Actual execution screen
This is the result of checking targets, sending a graceful termination signal, and verifying that no leftover processes remain.
!pkill command execution result: checking targets and graceful termination
Live production server
Source (HTML): https://games.aidebatehub.com/programs/linux/services/pkill.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments