SyntaxStudy
Sign Up
Linux / Bash Signals, kill, and Process Priorities
Linux / Bash Beginner 1 min read

Signals, kill, and Process Priorities

Signals are software interrupts sent to processes to notify them of events or request actions. Every signal has a name and a number. The most common are SIGTERM (15), which politely asks a process to terminate and allows cleanup, and SIGKILL (9), which forces immediate termination with no cleanup. Processes can catch and handle SIGTERM but cannot catch SIGKILL. The `kill` command sends a signal to a process identified by its PID. Despite the name, `kill` can send any signal, not just termination signals. `killall` and `pkill` send signals by process name rather than PID. The `killall -HUP nginx` pattern is commonly used to reload configuration files of daemons that support live reload via SIGHUP. Process priority is controlled through the "nice" value, ranging from -20 (highest priority) to +19 (lowest priority). Regular users can only increase the nice value (lower priority). The `nice` command starts a process at a given nice level, and `renice` changes the priority of an already-running process. Using `nice` for CPU-intensive background tasks prevents them from impacting interactive responsiveness.
Example
# ---- Sending signals with kill ----

# Gracefully terminate a process (SIGTERM = 15)
kill 1234
kill -15 1234
kill -SIGTERM 1234

# Force kill (SIGKILL = 9) — cannot be caught or ignored
kill -9 1234
kill -SIGKILL 1234

# Reload configuration (SIGHUP = 1)
kill -HUP $(cat /var/run/nginx.pid)

# Send signal by name
kill -l              # List all signal names

# ---- killall and pkill ----

# Kill all processes named 'python3'
killall python3

# Kill with SIGKILL
killall -9 runaway_script

# Kill processes matching a pattern
pkill -f "python3 myscript.py"

# Kill processes owned by a specific user
pkill -u bob

# ---- Process priority (nice / renice) ----

# Start a command at low priority (nice 10)
nice -n 10 tar -czf big_archive.tar.gz /data/

# Start at highest nice (least CPU impact)
nice -n 19 find / -name "*.log" -exec gzip {} \;

# Renice a running process (requires sudo for -20 to -1)
renice -n 5 -p 1234       # Set nice to 5 for PID 1234
sudo renice -n -5 -p 1234 # Increase priority (needs root)

# Renice all processes of a user
renice -n 10 -u alice