Linux Process Termination Signals Explained with Examples

Linux uses signals to communicate with processes, allowing them to terminate, pause, or handle specific events. Below are key termination signals, examples of how to use them, and their effects.


1. SIGINT (2): Interrupt Signal

  • Default Action: Terminate the process.
  • Trigger: Press Ctrl+C in the terminal.
  • Use Case: Gracefully stop a running command (e.g., a script or program).

Example:

# Start a long-running process
sleep 100
# Press Ctrl+C to send SIGINT and terminate it

Handling SIGINT in a Bash Script:

#!/bin/bash
trap 'echo "SIGINT caught! Exiting..."; exit' SIGINT
echo "Running... Press Ctrl+C to test SIGINT"
while true; do
  sleep 1
done

Output:

Running... Press Ctrl+C to test SIGINT
^CSIGINT caught! Exiting...

2. SIGQUIT (3): Quit Signal

  • Default Action: Terminate and generate a core dump.
  • Trigger: Press Ctrl+\ in the terminal.
  • Use Case: Debugging (generates a core dump for post-mortem analysis).

Example: Create a file send_sigquit.sh

#!/bin/bash

# Start a long-running process in the background
sleep 1000 &

# Capture its PID
pid=$!

# Optional: Wait 3 seconds
sleep 3

# Send SIGQUIT (signal 3)
kill -3 $pid

# Inform user
echo "Sent SIGQUIT (signal 3) to process with PID $pid"

Run the script:

sh ./send_sigquit.sh 

What it does:

  • Starts sleep 1000 in the background.
  • Waits 3 seconds.
  • Sends SIGQUIT (signal 3) to that background process using kill -3.
  • You’ll see confirmation that the signal was sent.

3. SIGTERM (15): Termination Signal

  • Default Action: Terminate the process.
  • Trigger: Sent by default with the kill command.
  • Use Case: Request graceful shutdown (allows cleanup).

Example: sigterm_handler.sh

#!/bin/bash

trap 'echo "SIGTERM received"; exit' SIGTERM

echo "Running... (PID $$)"
while true; do
    sleep 1
done

Run it in the background:

./sigterm_handler.sh &

Get its PID and send SIGTERM:

kill -TERM <pid>  # or kill -15 <pid>

You’ll see:

SIGTERM received

What trap Does:

  • It catches the signal (SIGTERM in this case).
  • Runs the code inside the quotes (echo ...; exit).
  • Helps you clean up resources before exiting.

4. SIGKILL (9): Kill Signal

  • Default Action: Forcefully terminate (cannot be caught/ignored).
  • Trigger: kill -9.
  • Use Case: Force-kill unresponsive processes.

Example:

# Force-kill a process
kill -9 $PID

5. SIGHUP (1): Hangup Signal

  • Default Action: Tell daemons or background processes to reload their config (like nginx, sshd, systemd, etc.)
  • Trigger: Sent when the terminal closes or a parent process dies.
  • Use Case: Reload configurations without restarting the process: (e.g., nginx -s reload).

Example: sighup_example.sh

#!/bin/bash

trap 'echo "SIGHUP received: Reloading config..."; # simulate config reload here' SIGHUP

echo "Script running... PID $$"
while true; do
    sleep 2
done

Run it in the background:

sh /sighup_example.sh &

Get its PID and send SIGHUP:

kill -1 <pid>  # or kill -HUP <pid> 

You’ll see:

SIGHUP received: Reloading config...

When to Use SIGHUP

Use Case Purpose
nginx, sshd, httpd Reload configuration on the fly
Custom scripts or daemons Trigger a config reload or safe restart
Background job via nohup Prevent SIGHUP from killing it

Comments

Popular Posts

Puppet Code Deploy Troubleshooting & Resolution Guide

Fix: SSH Permission Denied Issue | Real Solution