Bash unix signals: Difference between revisions
From wikinotes
(4 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
|} | |} | ||
</blockquote><!-- Documentation --> | </blockquote><!-- Documentation --> | ||
= Signal Keybindings = | |||
<blockquote> | |||
List signal keybindings | |||
<syntaxhighlight lang="bash"> | |||
stty -a | tr ';' '\n' | |||
# intr = ^C | |||
# quit = ^\ | |||
# erase = ^? | |||
# ... | |||
</syntaxhighlight> | |||
</blockquote><!-- Signal Keybindings --> | |||
= Send Signal = | = Send Signal = | ||
Line 15: | Line 27: | ||
kill -l # list all signals | kill -l # list all signals | ||
kill -9 ${PID} # send signal 9 (SIGKILL) | kill -9 ${PID} # send signal 9 (SIGKILL) | ||
kill -SIGINT ${PID} # send isgnal 2 (SIGINT) | kill -SIGINT ${PID} # send isgnal 2 (SIGINT) (same as ctrl-c) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Send Signal --> | </blockquote><!-- Send Signal --> | ||
Line 23: | Line 35: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
# Install callback for SIGINT and SIGTRAP | # Install callback for SIGINT and SIGTRAP | ||
on_signal() { | on_signal() { echo "received SIGINT or SIGTRAP!" } | ||
} | |||
trap on_signal SIGINT SIGTRAP | trap on_signal SIGINT SIGTRAP | ||
# Uninstall a trap | # Uninstall a trap | ||
trap - SIGINT SIGTRAP | trap - SIGINT SIGTRAP | ||
</syntaxhighlight> | |||
You can also trap on any process error (and implement behaviour similar to <code>set -e</code>) | |||
<syntaxhighlight lang="bash"> | |||
on_error() { | |||
echo "There was an error on line $LINENO" | |||
exit 1 | |||
} | |||
trap on_error ERR | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Callback on Signal --> | </blockquote><!-- Callback on Signal --> | ||
= Identifying Trapped Signal = | |||
<blockquote> | |||
See <code>bin/core/identify-unix-signal</code>.<br> | |||
I wrote a shellscript to identify the signals sent to the process. | |||
<syntaxhighlight lang="bash"> | |||
print_emitted_signal() { | |||
echo "SIGNAL IS: $1" | |||
} | |||
trap_all_signals() { | |||
for i in {1..64} ; do | |||
trap "print_emitted_signal $i" "$i" | |||
done | |||
} | |||
mainloop() { | |||
while true ; do | |||
echo "type 'q' to exit" | |||
read input | |||
if [[ "$input" == "q" ]] ; then | |||
break | |||
fi | |||
done | |||
} | |||
trap_all_signals | |||
mainloop | |||
</syntaxhighlight> | |||
</blockquote><!-- Identifying Trapped Signal --> |
Latest revision as of 13:50, 1 June 2023
Documentation
man 7 signal
https://man.archlinux.org/man/core/man-pages/signal.7.en wikipedia: unix signals https://en.wikipedia.org/wiki/Signal_(IPC)#POSIX_signals
Signal Keybindings
List signal keybindings
stty -a | tr ';' '\n' # intr = ^C # quit = ^\ # erase = ^? # ...
Send Signal
kill -l # list all signals kill -9 ${PID} # send signal 9 (SIGKILL) kill -SIGINT ${PID} # send isgnal 2 (SIGINT) (same as ctrl-c)
Callback on Signal
# Install callback for SIGINT and SIGTRAP on_signal() { echo "received SIGINT or SIGTRAP!" } trap on_signal SIGINT SIGTRAP # Uninstall a trap trap - SIGINT SIGTRAPYou can also trap on any process error (and implement behaviour similar to
set -e
)on_error() { echo "There was an error on line $LINENO" exit 1 } trap on_error ERR
Identifying Trapped Signal
See
bin/core/identify-unix-signal
.
I wrote a shellscript to identify the signals sent to the process.print_emitted_signal() { echo "SIGNAL IS: $1" } trap_all_signals() { for i in {1..64} ; do trap "print_emitted_signal $i" "$i" done } mainloop() { while true ; do echo "type 'q' to exit" read input if [[ "$input" == "q" ]] ; then break fi done } trap_all_signals mainloop