Signals | Linux

2024/01/03

The Linux command line interface uses signals as a means of communication between processes. Understanding Linux signals is essential for managing and interacting with running processes.

Understanding Linux Signals:

Linux signals are software interrupts that notify processes about specific events. Some commonly used signals include:

  • SIGHUP (1) :
    • Hangup signal, typically used to restart a process.
  • SIGINT (2) :
    • Signal interrupt — it tells the program to interrupt, usually triggered by pressing Ctrl + C.
  • SIGQUIT (3) :
    • Ctrl + D — directly terminates your program.
  • SIGKILL (9) :
    • Forcefully terminates a process.
    • Program assassination — shutdown immediately.
  • SIGTERM (15) :
    • Requests a process to terminate gracefully.
    • Signal terminate.
    • Typically issued before a system shutdown to close all programs properly.

General Rule: If You Don’t Know How to Exit or Terminate:

  1. First try Ctrl + C.
  2. If that doesn’t work, try Ctrl + D.
  • Demo Commands:
# Run yes in the background, continuously writing output to /dev/null (/dev/null is where you send anything you don't want to keep)
yes > /dev/null &
# Search for the process with the "yes" keyword (PID)
ps aux | grep yes

# Display the different types of signals
kill -l

# Send SIGTERM to the process with PID 1234
kill -15 1234

# Forcefully terminate the process with PID 5678
kill -9 5678
sudo kill -SIGKILL <process id>

# Restart a process by sending SIGHUP
kill -1 7890

# Interrupt a process (Ctrl + C)

Process Management:

By leveraging Linux signals, users can properly control running processes — performing shutdowns, restarts, and even emergency terminations when necessary. Understanding the differences between signals enables precise process management from the command line.