I have a bash script like this:
#!/bin/bash
startsomeservice &
echo $! > service.pid
while true; do
# dosomething in repeat all the time here
foo bar
sleep 5
done
# cleanup stuff on abort here
rm tmpfiles
kill $(cat service.pid)
the problem of this script is, that i cant abort it. If i press ctrl+c i just go into the next loop…
Is it possible to run a script like this but to have it abortable?
Since you are executing the script with Bash, you can do the following:
Please note that this behaviour is Bash specific, if you run it with Dash, for instance, you will see two differences:
SIGINTNote also that you will break a shell loop with a single
C-cwhen you execute the loop directly from an interactive prompt, even if you’re running Bash. See this detailed discussion aboutSIGINThandling from shells.