Skip to content

Instantly share code, notes, and snippets.

@shinkou
Created May 2, 2024 04:34
Show Gist options
  • Save shinkou/14ab16aa780b2453f70c573afe51707b to your computer and use it in GitHub Desktop.
Save shinkou/14ab16aa780b2453f70c573afe51707b to your computer and use it in GitHub Desktop.
A snippet to showcase how to trap and control signals in Bash
#!/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