I have a bash script which does some work, which is done fairly quick. It should then idle until the user decides to terminate it, followed by some clean-up code.
This is why I trap the CTRL+c event with the following code:
control_c()
{
cleanup
exit 0
}
trap control_c SIGINT
But as my script is done quite quickly I never get to purposely terminate it, so it never gets to trap the CTRL+c and run the clean-up code.
I figured I could implement an endless do while
loop, with sleep
at the end of the script, but I assume there is a better solution.
How can I idle a script in bash, expecting the CTRL+c event?
trap cleanup 0
and you're no longer depending on details of how you exit, as long as it isn't something like SIGKILL that can't be trapped at all. – Ithunniptables
rules which should only be in effective until the user decides it is no longer required. – Methuselah