Issue
Suppose that sleep 180
is a command that requires some time.
I have a script that looks like this
#!/bin/bash
echo "Before sleep"
sleep 180 # I want to stop this
echo "After sleep"
exit 0
I want that, when the user press CTRL+C while sleep
is executing, only this is terminated so that the user sees "After sleep".
Solution
I found a solution while I was writing here.
The solution was writing on top of the script this simple signal handler:
#!/bin/bash
# subroutine
on_int(){
echo
echo "Stopping current command..."
}
# register signal handler subroutine for SIGINT
trap on_int SIGINT
# ...
In this way the sleep
command is stopped but the script continues whit the next commands.
Answered By - Enrico R. Answer Checked By - Senaida (WPSolving Volunteer)