drfrog has asked for the wisdom of the Perl Monks concerning the following question:

hey

what im trying to do is build a perl script using Schedule::Cron and i want to run it like
>perl foo.pl &

i want to use $SIG{} events to trap if this script bails for some reason
here is the relevant piece of code taken from perlipc perlman:perlipc
sub catch_zap { my $signame = shift; die "Somebody sent me a SIG$signame"; } $SIG{INT} = \&catch_zap;
ive been trying to test with various variations of
'kill -9 PID'

what would you monks suggest needs to be trapped so i would know when my script fails?

Replies are listed 'Best First'.
Re: perl script as a running process
by fglock (Vicar) on Jun 30, 2003 at 19:15 UTC

    Some time ago I read that 'kill -9 PID' is not trappable. It kills the process before it can properly die.

    update: from perlipc:

    Some signals can be neither trapped nor ignored, such as the KILL and STOP (but not the TSTP) signals.

      Correct. Signal 9 is SIGKILL, which is not actually sent to the process; it tells the kernel to terminate it without remorse. If you're trying to catch INT, send INT (kill -2) or for TERM, -15.

      --isotope
Persistent Perl
by barbie (Deacon) on Jul 01, 2003 at 11:25 UTC
    Nothing to do with the signal question, but you might be interested in PPerl for your running process. Seems to be gaining in popularity. Rather like mod_perl for stand alone scripts.

    --
    Barbie | Birmingham Perl Mongers | http://birmingham.pm.org/

Re: perl script as a running process
by nimdokk (Vicar) on Jun 30, 2003 at 19:08 UTC
    One of the things that I have is a step to capture the actual error messages that might go to STDERR. I try to capture all the output of a program into a logfile. I use an END block to determine whether the program exits cleanly or if its generated an error. Different exits determine differnt behaviors. For instance, when the program dies, it sends out an alert to a pager and makes a copy of the log so that I (or someone else) can look at the errored log and see what blew up (in case the actual log gets overwritten before we can get dialed in). You might look at using $SIG {__DIE__}. Just my own 2 cents though :-)


    "Ex libris un peut de tout"
Re: perl script as a running process
by drfrog (Deacon) on Jun 30, 2003 at 19:40 UTC
    so if i catch -2 and -15 I should be fine?
      You should be OK as long as you don't try to catch -22 ;-)


      "Ex libris un peut de tout"