kill Command

The kill command sends signals to processes in Linux and Unix systems. Despite its name, it doesn't always "kill" processes - it sends various signals that can terminate, pause, resume, or communicate with processes for system management and troubleshooting.

Syntax

kill [signal] PID...

Description

The kill command sends signals to processes identified by their Process ID (PID). Signals are software interrupts that allow the operating system and users to communicate with running processes. The default signal is SIGTERM (15), which requests graceful termination.

Key concepts:

  • Signals are numbered interrupts sent to processes
  • Processes can handle, ignore, or be forced to respond to signals
  • SIGTERM allows graceful shutdown with cleanup
  • SIGKILL forces immediate termination without cleanup
  • Some signals can be caught and handled by programs

Recommended Signal Sequence

  1. SIGTERM (15): Request graceful shutdown - try this first
  2. Wait 5-10 seconds for process to clean up and exit
  3. SIGKILL (9): Force immediate termination if needed

This approach allows programs to save data and clean up resources before termination.

Common Signals

Signal Number Description Can be Caught?
SIGTERM 15 Terminate (graceful shutdown) Yes
SIGKILL 9 Kill (immediate termination) No
SIGINT 2 Interrupt (Ctrl+C) Yes
SIGHUP 1 Hangup (reload config) Yes
SIGSTOP 19 Stop (pause process) No
SIGCONT 18 Continue (resume process) No
SIGUSR1 10 User-defined signal 1 Yes

Signal Details

  • Catchable signals: Programs can handle these and perform cleanup
  • Non-catchable signals: Cannot be ignored or handled (SIGKILL, SIGSTOP)
  • Default action: What happens if the signal isn't handled
  • Signal names: Can use names (SIGTERM) or numbers (15)

⚠️ Important Safety Notes

  • Always try SIGTERM first: Allows graceful shutdown and data saving
  • Use SIGKILL sparingly: Can cause data loss and corruption
  • Check process ownership: You can only kill your own processes (unless root)
  • Be careful with system processes: Killing critical processes can crash the system

Examples

Basic process termination

kill 1234 # Send SIGTERM to process 1234
kill -15 1234 # Same as above (explicit SIGTERM)
kill -TERM 1234 # Same using signal name
kill -9 1234 # Force kill with SIGKILL

Terminate processes using different signal types

Find and kill processes

# Find process ID first
ps aux | grep firefox
kill 5678 # Kill the found PID

# Or use pgrep/pkill
pgrep firefox # Find PIDs by name
pkill firefox # Kill by name directly
killall firefox # Kill all instances

Locate and terminate processes by name or criteria

Graceful shutdown sequence

kill 1234 # Try graceful termination
sleep 5 # Wait for cleanup
ps -p 1234 > /dev/null && kill -9 1234 # Force kill if still running

# One-liner version
kill 1234 || (sleep 5 && kill -9 1234)

Implement safe termination with fallback to force kill

Multiple processes

kill 1234 5678 9012 # Kill multiple processes
kill -9 $(pgrep apache2) # Force kill all Apache processes
pkill -f "python script.py" # Kill by full command line
killall -9 chrome # Force kill all Chrome processes

Terminate multiple processes efficiently

Process control signals

kill -STOP 1234 # Pause process (Ctrl+Z)
kill -CONT 1234 # Resume paused process
kill -HUP 1234 # Reload configuration
kill -USR1 1234 # Send user-defined signal

Use signals for process control beyond termination

Verify termination

ps -p 1234 # Check if process still exists
if ps -p 1234 > /dev/null; then
echo "Process still running"
else
echo "Process terminated"
fi

Confirm that processes have been successfully terminated

🚨 DANGEROUS: System-Critical Processes

NEVER kill these processes unless you know exactly what you're doing:

  • PID 1 (init/systemd): System initialization - will crash the system
  • Kernel threads [in brackets]: Core system functions
  • SSH daemon (sshd): Will lock you out of remote systems
  • Display manager: Will kill your desktop session

💡 Tips and Best Practices

  • Always try SIGTERM first: Gives processes chance to clean up
  • Wait before SIGKILL: Allow 5-10 seconds for graceful shutdown
  • Use pkill for names: Easier than finding PIDs manually
  • Check process ownership: You can only kill your own processes
  • Use ps to verify: Confirm termination was successful
  • Be careful with wildcards: Double-check what you're killing

Common Use Cases

  • Hung application: pkill firefox && sleep 5 && pkill -9 firefox
  • Restart service: sudo pkill apache2 && sudo systemctl start apache2
  • Stop runaway process: kill -STOP PID (pause), then investigate
  • Reload configuration: sudo kill -HUP $(pgrep nginx)
  • Clean shutdown script: kill $PID || (sleep 10 && kill -9 $PID)

Troubleshooting

  • "Operation not permitted": You don't own the process or need sudo
  • "No such process": Process already terminated or PID doesn't exist
  • Process won't die: Try SIGKILL (-9) or check if it's a zombie
  • Zombie processes: Cannot be killed, parent process must reap them
  • Process respawns: Check if managed by systemd or init system

See also