Fundamentals 7 min read

Understanding Linux Signals, kill Command, and trap Command

This article explains the types and default actions of Linux signals, distinguishes between standard and real‑time signals, and demonstrates how to use the kill and trap commands with practical shell script examples for signal handling.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Understanding Linux Signals, kill Command, and trap Command

1. Linux Signal Types

Signals are software‑level simulations of hardware interrupts; sending a signal to a process triggers a corresponding handler.

Processes can respond to a signal by ignoring it (except SIGKILL and SIGSTOP, which cannot be ignored), catching it with a custom handler, or performing the default action defined by the kernel.

The way a signal is handled depends on the API function used to deliver it.

Linux defines many signals; the most common ones are highlighted in the table below.

Number

Signal Name

Default Action

Description

1

SIGHUP

Terminate

Terminate process, hang up

2

SIGINT

Terminate

Keyboard interrupt (Ctrl+C)

3

SIGQUIT

CoreDump

Keyboard quit (Ctrl+\)

9

SIGKILL

Terminate

Immediately stop process; cannot be caught or ignored

15

SIGTERM

Terminate

Graceful termination; can be caught

CoreDump means the kernel writes a core file for debugging when a process aborts unexpectedly.

Linux supports two categories of signals:

Standard signals (1‑31) are non‑real‑time, not queued, and may be lost if sent repeatedly.

Extended (real‑time) signals (32‑64) are reliable and queued, preserving the number of occurrences.

Understanding a few common signals is sufficient for most tasks.

2. kill Command

The kill command sends signals to processes.

Syntax:

kill [-s sigspec | -n signum | -sigspec] pid | jobspec ...
kill -l [sigspec]

Options: -s – specify signal name -n – specify signal number -l – list signal names

Example:

# Send termination signal to a process</code><code>kill -s SIGTERM pid</code><code># or using number</code><code>kill -n 15 pid</code><code># or short form</code><code>kill -15 pid

3. trap Command

The trap command defines how a shell script should react to received signals.

Syntax: trap [-lp] [[arg] signal_spec ...] Common actions after catching a signal:

Remove temporary files

Ignore the signal

Prompt the user before terminating the script

Examples:

#!/bin/bash</code><code>trap "" 2    # Ignore SIGINT (Ctrl+C)</code><code>for i in {1..10}; do</code><code>    echo $i</code><code>    sleep 1</code><code>done
#!/bin/bash</code><code>trap "echo 'exit...'; exit" 2</code><code>for i in {1..10}; do</code><code>    echo $i</code><code>    sleep 1</code><code>done
#!/bin/bash</code><code>trap "func" 2</code><code>func() {</code><code>    read -p "Terminate the process? (Y/N): " input</code><code>    if [ "$input" == "Y" ]; then</code><code>        exit</code><code>    fi</code><code>}</code><code>for i in {1..10}; do</code><code>    echo $i</code><code>    sleep 1</code><code>done

These examples show how to ignore, handle, or prompt the user when a signal like SIGINT is received.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

linuxShelloskillsignalstrap
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.