Bash unix signals: Difference between revisions
From wikinotes
Line 52: | Line 52: | ||
</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 --> |
Revision as of 15:15, 17 December 2022
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)
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