How to kill a process in Linux: kill PID (default SIGTERM, graceful shutdown), kill -9 PID (SIGKILL, immediate). pkill pattern and killall name kill by process name. pgrep and pidof find PIDs. The team that uses SIGTERM first (graceful) and SIGKILL only as last resort has correct process management.
Table of contents
- Find the PID first
- kill by PID
- pkill by name
- killall
- What SIGTERM does
- When to use SIGKILL (-9)
- Kill all processes by user
- Kill a process group
- Common patterns
- FAQ
Find the PID first
# By exact name
pgrep nginx
# Output: 1234 5678
# By pattern
pgrep -f 'python.*myapp'
# Or
pidof nginx
# Detailed view
ps aux | grep nginx
The team that uses pgrep or pidof finds PIDs without scrolling through ps.
kill by PID
kill 1234 # SIGTERM (graceful)
kill -TERM 1234 # same as above
kill -15 1234 # also SIGTERM (signal number)
kill -9 1234 # SIGKILL (force, no cleanup)
The team that uses SIGTERM first has the process clean up resources. SIGKILL is last resort.
pkill by name
pkill nginx
pkill -f 'python.*myapp'
# Or with full signal control
pkill -TERM -f myapp
pkill -KILL nginx # same as SIGKILL
The team that uses pkill doesn’t need to find PIDs manually.
killall
killall nginx
killall -9 python # SIGKILL all python processes (careful!)
The team that uses killall is faster than pkill for exact matches. pkill is more flexible (regex matching).
What SIGTERM does
The process receives SIGTERM and can:
- Close file handles.
- Flush buffers.
- Send final responses to clients.
- Remove temp files.
- Exit cleanly (exit code 0).
The team that has well-behaved services (Node, Java, Go) has clean shutdowns on SIGTERM.
When to use SIGKILL (-9)
Use only when:
- Process is unresponsive to SIGTERM.
- Process is in an infinite loop.
- Process won’t respond to Ctrl-C.
- You have already tried graceful.
The team that uses -9 immediately has data loss risk (no cleanup). The team that tries SIGTERM first has correct behavior.
Kill all processes by user
pkill -u username
The team that uses this has admin user logout (kills all user processes).
Kill a process group
# Start a process group
setsid myapp &
# Kill the entire group
kill -TERM -- -<pgid>
The team that uses process groups has child processes killed together.
Common patterns
# Restart a service
sudo systemctl restart myapp
# Find and kill a stuck process
PID=$(pgrep -f myapp)
kill $PID
# If still alive after 5s
kill -9 $PID
# Kill all instances of an app
pkill -TERM -f myapp; sleep 5; pkill -KILL -f myapp
The team that has these patterns in scripts has automated recovery.
FAQ
What’s the difference between SIGTERM and SIGKILL?
SIGTERM (15): graceful, process can clean up. SIGKILL (9): immediate, no cleanup. The team that uses SIGTERM first has correct behavior.
Can a process ignore SIGKILL?
No. The kernel handles it directly. The team that has a stuck process has SIGKILL as last resort.
What’s the difference between kill, pkill, killall?
kill: by PID. pkill: by pattern (regex). killall: by exact name.
How do I kill a process I can’t see?
ps auxf shows process tree. htop has interactive tree view. The team that uses htop has visual process management.
Why does my process keep respawning?
It’s managed by systemd, supervisord, or a process manager. The team that has a stuck daemon kills the manager, not the individual process.
If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.
Useful related references: