Created
March 21, 2020 01:37
-
-
Save davidlares/7c06b1f9c5aff707f0689fc9e1bd3577 to your computer and use it in GitHub Desktop.
Handling bash Signals events for program interruptions or a simple kill
This file contains hidden or 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 | |
# 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