I have a daemon running on Ubuntu written in Perl, it is now single threaded. When it starts up it does the usual Proc::Daemon stuff and then goes into a while loop bounded by a boolean. The daemon starts up fine with "service daemon start". But when I want to kill it with "service daemon stop" it doesn't stop.
Stopping is supposed to happen by flipping the boolean using a signal handler:
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signalHandler;
sub signalHandler {
$continue = 0;
}
Unfortunately the main loop also has a sleep in it. So the daemon doesn't end until the sleep is over. I would like the daemon to end immediately. But I don't want to set a very low sleep time. In fact I want to be able to set a sleep time of several hours if that is what it determines it should do.
What is the best way to do this?
I was thinking about calling some sort of blocking function inside the main loop instead of a sleep. This blocking function would not return until some thread has died. Then in the signal handler I would simply kill that sleeping thread. Is this a good approach? And how would I do that, I've never done multi-threading in Perl before.
thanks for any help