This is a supplement or addition to other answers already given, including these:
- List of Kill Signals [duplicate]_
- What causes various signals to be sent?
For an even more-detailed explanation of most of these signals, see Wikipedia: https://en.wikipedia.org/wiki/Signal_(IPC)
One of the most-common signals is the SIGINT
(or just INT
) program interrupt signal sent by pressing Ctrl + C while a process is running.
From the Wikipedia article above:
SIGINT
"Signal interrupt"
The SIGINT
signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Ctrl+C, but on some systems, the "delete" character or "break" key can be used.[12]
How to manually send any signal to any running process
What causes various signals to be sent [to a running process]?
In addition to other programs or keystrokes causing signals to be sent, you do!
Run kill -l
or kill --list
to list all signals you can send to a running process.
Example run and output:
$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
So, to send the "alarm" signal (as SIGALRM
, ALRM
, or signal number 14
), for example, you can do any one of the following:
kill -SIGALRM <pid>
kill -ALRM <pid>
kill -14 <pid>
Example kill
commands to send the alarm signal to pid 123456
:
kill -SIGALRM 123456
kill -ALRM 123456
kill -14 123456
Read this from man kill
(emphasis added):
DESCRIPTION
The default signal for kill
is TERM
. Use -l
or -L
to list available signals. Particularly useful signals include HUP
, INT
, KILL
, STOP
, CONT
, and 0
. Alternate signals may be specified in three ways: -9
, -SIGKILL
or -KILL
. Negative PID values may be used to choose whole process groups; see the PGID column in ps
command output. A PID of -1
is special; it indicates all processes except the kill process itself and init.
To find any process ID (pid) of interest, run this:
ps aux | grep "some program name string or regular expression to search for"
Look for the signal number in the far left of the output row. The 2nd column from the left is the PID (Process ID) number.
How to trap any signal in your program
1. In Bash
To "trap", or catch and act on a signal in a custom bash program you can do the following (this traps the SIGALRM
signal):
trap <any_comand_to_run_when_signal_is_received> SIGALRM
Here is a common trap I use to capture the Ctrl + C "interrupt" (SIGINT
, INT
, or signal number 2
) signal in my custom bash programs:
# Trap the Ctrl + C SIGINT signal from this point onward in the bash program so
# that whenever the user presses Ctrl + C, this program prints `Ctrl + C` and
# then exits with exit code 2 (change that to any exit code you'd like to
# return when Ctrl + C is pressed!).
trap 'printf "%s\n" " Ctrl + C"; exit 2' SIGINT
See: Server Fault: Bash Loop - How to stop the loop when I press Control-C inside a command?.
2. In C and C++
To trap and act on any given signal in C or C++ on Linux you can use sigaction()
(best) or signal()
(not as good, but part of the C standard). See my comprehensive answer on that here, with detailed usage examples: Stack Overflow: What is the difference between sigaction and signal?
Examples of sending (via kill
) and trapping (via trap
) signals in bash
Search for the trap
command (which receives and acts on a signal) and kill
command (which sends a signal to a process) in these programs:
- See this bash Centipede game:
- Ask Ubuntu: Command-line snake game?
- In my eRCaGuy_hello_world repo here: centipede_game.sh