I want a script run in the background, take some actions for every hour. It should also quit if receiving a signal. And at last, it should perform some clean-up actions before quitting.
Based on the concept, I wrote something like the following:
trap 'clean_up; exit' SIGTERM while true do someActions sleep 1h done
I ran the script by
I kill the script by
kill -SIGTERM <pid>
It turned out the script didn't work as expected. I think the reason is that you can kill a sleeping process only by
SIGKILL signal. But on the other hand, you cannot trap a
SIGKILL signal to do some cleanup before quitting.
So how can I make a process wait in the background and be able to quit when it receives a signal other than