12 ‐ SIGNALS - CloudScope/DevOpsWithCloudScope GitHub Wiki

Signals in Linux are a form of inter-process communication used to notify processes that a specific event has occurred. They allow processes to handle or respond to events like interrupts, terminations, or other specific actions. Here’s an overview of signals, their types, usage, and handling in Linux:

Basics of Signals

  1. Definition:

    • A signal is a limited form of communication used to notify a process that a particular event has occurred.
    • Signals can be sent by the kernel, other processes, or by the process itself.
  2. Signal Characteristics:

    • Each signal has a unique integer identifier (signal number) and a name.
    • Signals can cause a process to stop, continue, terminate, or perform a specific handler routine.
    • Signals are asynchronous, meaning they can be sent and received at any time during a process's execution.

Common Signals

Some of the most commonly used signals include:

Signal Number Description Default Action
SIGHUP 1 Hangup detected on controlling terminal or death of controlling process Terminate
SIGINT 2 Interrupt from keyboard (Ctrl+C) Terminate
SIGQUIT 3 Quit from keyboard (Ctrl+) Core dump
SIGKILL 9 Kill signal (cannot be caught or ignored) Terminate
SIGTERM 15 Termination signal (default kill signal) Terminate
SIGSTOP 19 Stop process (cannot be caught or ignored) Stop
SIGCONT 18 Continue if stopped Continue
SIGALRM 14 Timer signal from alarm Terminate
SIGUSR1 10 User-defined signal 1 Terminate
SIGUSR2 12 User-defined signal 2 Terminate
SIGCHLD 17 Child process stopped or terminated Ignore
SIGSEGV 11 Invalid memory access (segmentation fault) Core dump

Sending Signals

  1. Using kill Command:

    • kill is used to send signals to processes.
    • Syntax:
      kill -SIGNAL PID
      
    • Example: Send SIGTERM to process with PID 1234:
      kill -TERM 1234
      
    • You can also use signal numbers:
      kill -9 1234  # Sends SIGKILL to PID 1234
      
  2. Using killall Command:

    • Sends signals to processes by name.
    • Example: Send SIGKILL to all instances of processname:
      killall -9 processname
      
  3. Using pkill Command:

    • Similar to killall but with more flexible pattern matching.
    • Example: Send SIGTERM to all processes matching pattern:
      pkill pattern
      
  4. Using kill Function in Code:

    • In C programming, signals can be sent using the kill function.
    • Example:
      kill(pid, SIGINT);  // Send SIGINT to process with pid
      

Handling Signals

  1. Default Actions:

    • Each signal has a default action, such as terminate, ignore, stop, or core dump.
  2. Custom Signal Handlers:

    • Processes can override the default action by defining a signal handler function.
    • In shell scripts, you can trap signals using the trap command.
    • Example: Trap SIGINT (Ctrl+C) in a script:
      trap "echo 'Interrupted!'; exit" SIGINT
      
  3. Ignoring Signals:

    • Some signals can be ignored using signal handlers.
    • Example: Ignore SIGINT:
      trap '' SIGINT
      
  4. Uncatchable Signals:

    • Signals like SIGKILL (9) and SIGSTOP (19) cannot be caught, ignored, or handled by the process.

Summary

  • Signals are used for inter-process communication and managing process behavior.
  • Common signals include SIGINT (interrupt), SIGTERM (terminate), and SIGKILL (force kill).
  • Signals can be sent using commands like kill, killall, and pkill.
  • Processes can define custom handlers for most signals, except for those like SIGKILL and SIGSTOP which are uncatchable.