Skip to content

Instantly share code, notes, and snippets.

@karlsebal
Last active July 30, 2022 05:50
Show Gist options
  • Save karlsebal/d6c226ab2cb610d5f075b029c52ba2ba to your computer and use it in GitHub Desktop.
Save karlsebal/d6c226ab2cb610d5f075b029c52ba2ba to your computer and use it in GitHub Desktop.
clean exit in bash
#!/bin/sh
# How To Die Gracefully
cleanup() {
echo clean up…
}
die() {
echo dying gracefully
kill $proc
exit 0
}
bg(){
while :; do
echo bla
sleep 3
done
}
trap cleanup EXIT
trap die INT
bg&
proc=$!
while :; do
echo blubb
sleep 13
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment