Mastering Linux Signals: How to Use kill and trap Commands Effectively
This article explains Linux signal concepts, the three ways a process can handle signals, provides a concise reference of common signals with their default actions, and demonstrates practical usage of the kill and trap commands through clear shell script examples.
1. Linux Signal Basics
A signal is a software‑level simulation of an interrupt; sending a signal to a process triggers a handler function. A process can respond to a signal in three ways: ignore it (except SIGKILL and SIGSTOP, which cannot be ignored), catch it with a custom handler, or perform the default action defined by the kernel.
The kernel decides which response to use based on the API function used to set the handler.
Common signals (default action shown) include:
1 SIGHUP – terminate (hang up)
2 SIGINT – terminate (Ctrl+C)
3 SIGQUIT – core dump (Ctrl+\)
4 SIGILL – core dump (illegal instruction)
5 SIGTRAP – core dump (debug trap)
6 SIGABRT – core dump (abort)
7 SIGBUS – core dump (invalid address)
8 SIGFPE – core dump (floating‑point exception)
9 SIGKILL – terminate immediately (cannot be caught or ignored)
10 SIGUSR1 – terminate (user‑defined, e.g., Nginx reload)
11 SIGSEGV – core dump (invalid memory reference)
12 SIGUSR2 – terminate (user‑defined)
13 SIGPIPE – terminate (broken pipe)
14 SIGALRM – terminate (timer alarm)
15 SIGTERM – terminate (graceful shutdown, can be caught)
16 SIGSTKFLT – terminate (stack fault)
17 SIGCHLD – ignore (child process status change)
18 SIGCONT – ignore (continue stopped process)
19 SIGSTOP – stop (cannot be caught or ignored)
20 SIGTSTP – stop (Ctrl+Z)
21 SIGTTIN – stop (background read from terminal)
22 SIGTTOU – stop (background write to terminal)
23 SIGURG – ignore (out‑of‑band data)
24 SIGXCPU – core dump (CPU time limit exceeded)
25 SIGXFSZ – core dump (file size limit exceeded)
26 SIGVTALRM – terminate (virtual timer alarm)
27 SIGPROF – terminate (profiling timer)
28 SIGWINCH – ignore (window size change)
29 SIGIO – terminate (I/O ready)
30 SIGPWR – terminate (power failure)
31 SIGSYS – core dump (bad system call)
When a program crashes abnormally, the kernel writes a core dump file containing the process's memory image for debugging.
Linux defines two signal families: standard signals (1‑31, non‑realtime, not queued, may be lost) and real‑time signals (32‑64, reliable, queued).
2. Using the kill Command
The kill utility sends a specified signal to one or more processes.
kill [-s sigspec | -n signum | -sigspec] pid ...
kill -l [sigspec]Options: -s – specify signal name -n – specify signal number -l – list signal names (1‑31 by default)
Examples:
kill -s SIGTERM 1234 # send SIGTERM by name
kill -n 15 1234 # send signal 15 (SIGTERM) by number
kill -15 1234 # shorthand
kill -SIGKILL 1234 # force termination3. Using the trap Command
trapdefines how a shell script should react to received signals. trap [-lp] [[arg] signal_spec ...] Typical actions after catching a signal:
Remove temporary files
Ignore the signal
Prompt the user before exiting
Example 1 – ignore Ctrl+C (SIGINT) in a loop:
#!/bin/bash
trap "" 2 # 2 is SIGINT, empty arg means do nothing
for i in {1..10}; do
echo $i
sleep 1
doneExample 2 – exit with a message when Ctrl+C is pressed:
#!/bin/bash
trap "echo 'exit...'; exit" 2
for i in {1..10}; do
echo $i
sleep 1
doneExample 3 – ask the user whether to terminate the loop:
#!/bin/bash
trap "func" 2
func() {
read -p "Terminate the process? (Y/N): " input
if [ "$input" == "Y" ]; then
exit
fi
}
for i in {1..10}; do
echo $i
sleep 1
doneThese examples illustrate how to control script termination and cleanup using signals.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
