Bash unix signals: Difference between revisions

From wikinotes
Line 21: Line 21:
= Callback on Signal =
= Callback on Signal =
<blockquote>
<blockquote>
Install callback for SIGINT and SIGTRAP
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
# Install callback for SIGINT and SIGTRAP
on_signal() {
on_signal() {
     echo "received SIGINT or SIGTRAP!"
     echo "received SIGINT or SIGTRAP!"
}
}
trap on_signal SIGINT SIGTRAP
trap on_signal SIGINT SIGTRAP
</syntaxhighlight>


Uninstall a trap
# Uninstall a trap
<syntaxhighlight lang="bash">
trap - SIGINT SIGTRAP
trap - SIGINT SIGTRAP
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Callback on Signal -->
</blockquote><!-- Callback on Signal -->

Revision as of 12:24, 17 July 2021

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

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 SIGTRAP