I have built a perl script and have it running fine as a windows service using srvany. The service runs and does everything I want except, when the service is stopped by a "net stop" or by stopping the service via the control panel "services" app using "stop" or "restart" my END{} block does not fire. I'm using
sigtrap to capture untrapped, normal-signals and error-signals and this works for most situations even if the server is brought down. The code below illustrates the issue, of course it must be added as a service in order to test.
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use sigtrap qw(die untrapped normal-signals error-signals);
INIT {
my $me = basename($0);
sendmail('me@mydomain.com', "Service: $me Started", 'Service Start
+ed')
}
END {
my $me = basename($0);
sendmail('me@mydomain.com', "Service: $me Ended", 'Service Ended')
}
Although the service otherwise works it's a real nuisance not being able to trap these stop signals, I'd really prefer to do clean up on the stop rather than do it when the service starts.
Any thoughts are appreciated.
TIA