Skip to content

Instantly share code, notes, and snippets.

@davidlares
Created March 21, 2020 01:37
Show Gist options
  • Save davidlares/7c06b1f9c5aff707f0689fc9e1bd3577 to your computer and use it in GitHub Desktop.
Save davidlares/7c06b1f9c5aff707f0689fc9e1bd3577 to your computer and use it in GitHub Desktop.
Handling bash Signals events for program interruptions or a simple kill
#!/bin/bash
# traps (look for the ocurrence of something - event, commands, signals -> redirect reaction) and signals
clear
# what to do, what is it we are traping
trap 'echo " - Please Press Q to Exit ..."' SIGINT SIGTERM SIGTSTP # interrupt or simple kill
while [ "$CHOICE" != "Q" ] && [ "$CHOICE" != "q" ]; do
echo "Main Menu"
echo "========="
echo "1) Choice One"
echo "2) Choice Two"
echo "3) Choice Three"
echo "Q) Quit/Exit"
echo ""
read CHOICE # evaluate the choice option
clear
done
# the trap message will appear after CTRL+ C
# ps aux | grep [file]
# bg [Pid] -> suspend the execution of that script -> in anticipation will apply some message (not running anymore)
# kill -9 [Pid] -> killing from kernel - cannot be traped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment