Terminal GuideTerminal Guide

kill Command Guide

The kill command sends signals to processes, most commonly to terminate them. Learn how to safely stop processes and use different signals.

5 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

kill PIDTerminate (SIGTERM)
kill -9 PIDForce kill (SIGKILL)
kill -lList signals

Signals

-15 (TERM)Graceful terminate
-9 (KILL)Force kill
-1 (HUP)Reload config

By Name

pkill nameKill by pattern
killall nameKill by exact name
pkill -u userKill user processes

Find PID

pgrep nameGet PID by name
pidof nameGet exact PID
ps aux | grep nameSearch processes

Downloadable Image Preview

Failed to generate preview

Basic Usage

The kill command sends a signal to a process. By default, it sends SIGTERM (signal 15), which asks the process to terminate gracefully.

bash
# Terminate process by PID
kill 1234

# Same as above (explicit SIGTERM)
kill -15 1234
kill -TERM 1234

Common Signals

Frequently Used Signals

SIGTERM (15)Graceful termination (default)
SIGKILL (9)Force kill (cannot be caught)
SIGHUP (1)Hangup - often used to reload config
SIGINT (2)Interrupt (like Ctrl+C)
SIGSTOP (19)Pause process
SIGCONT (18)Continue paused process
bash
# List all available signals
kill -l

Graceful vs Force Kill

Graceful termination (SIGTERM)

Allows the process to clean up, save data, and exit properly.

bash
kill 1234

Force kill (SIGKILL)

Immediately terminates the process. Use only when SIGTERM doesn't work.

bash
kill -9 1234
# or
kill -KILL 1234
Warning
Always try kill (SIGTERM) first. Only use kill -9 when the process is unresponsive, as it doesn't allow cleanup.

Kill by Name: pkill and killall

pkill - pattern-based killing

bash
# Kill by process name
pkill nginx

# Kill by exact name
pkill -x nginx

# Kill processes owned by user
pkill -u username

# Force kill by name
pkill -9 nginx

killall - kill by exact name

bash
# Kill all processes with exact name
killall nginx

# Force kill all
killall -9 nginx

# Interactive mode (confirm each)
killall -i nginx
Tip
pkill matches partial names by default, while killall requires exact matches. Use pgrep first to verify which processes will be affected.

Finding Process IDs

bash
# Using pgrep
pgrep nginx
pgrep -a nginx  # Show full command line

# Using ps and grep
ps aux | grep nginx

# Using pidof
pidof nginx

Practical Examples

Kill multiple processes

bash
kill 1234 5678 9012

Kill all user processes

bash
pkill -u username

Reload configuration (HUP signal)

bash
# Reload nginx config
kill -HUP $(pgrep nginx)

# Or using pkill
pkill -HUP nginx

Pause and resume a process

bash
# Pause
kill -STOP 1234

# Resume
kill -CONT 1234

Kill unresponsive terminal process

bash
# From another terminal
ps aux | grep stuck_process
kill -9 <PID>

Kill all background jobs

bash
# In current shell
kill $(jobs -p)

Kill process tree

bash
# Kill process and all its children
pkill -P 1234    # Kill children
kill 1234        # Then kill parent

Safe Termination Pattern

Follow this pattern for safe process termination:

bash
# 1. Try graceful shutdown
kill <PID>

# 2. Wait a few seconds
sleep 5

# 3. Check if process is still running
ps -p <PID>

# 4. If still running, force kill
kill -9 <PID>

Signals Reference

All Common Signals

1 (SIGHUP)Hangup - terminal closed or reload config
2 (SIGINT)Interrupt from keyboard (Ctrl+C)
3 (SIGQUIT)Quit from keyboard (Ctrl+\)
9 (SIGKILL)Kill immediately (cannot be caught)
15 (SIGTERM)Termination request (default)
18 (SIGCONT)Continue stopped process
19 (SIGSTOP)Stop process (cannot be caught)
20 (SIGTSTP)Stop from terminal (Ctrl+Z)

Summary

kill is essential for process management. Key takeaways:

  • Use kill PID for graceful termination
  • Use kill -9 PID only when necessary
  • Use pkill name to kill by process name
  • Use pgrep name to find PIDs first
  • Use kill -HUP to reload configs

Related Articles