Created
May 2, 2024 04:34
-
-
Save shinkou/14ab16aa780b2453f70c573afe51707b to your computer and use it in GitHub Desktop.
A snippet to showcase how to trap and control signals in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
main() | |
{ | |
echo "PID: $$" | |
for s in SIGHUP SIGINT SIGQUIT SIGTERM EXIT; do | |
if [ SIGTERM = $s ]; then | |
trap "echo '$s is caught'; exit" $s | |
else | |
trap "echo '$s is caught'" $s | |
fi | |
done | |
for i in {1..60}; do | |
echo $i | |
sleep 1 | |
done | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment