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

I have two perl scripts on a server that tail a file on another system and writing records to a db. One is always active, and one is always backup. If the first server fails, the second kicks on and the second script begins tailing that backup box for records. This is done using IO::Socket. The section I am posting below is creating the socket I believe. But would like more understanding on what it's doing. Like what is it looking for with the TERM, INT, PIPE, CHLD? What is the TERM, HUP, INT all about? Is that how it determines if there was a user sent command to shutdown, or if it was a crash, or if it just shut itself down? Thank you for any input
$SIG{TERM}=$SIG{INT}=$SIG{PIPE}=$SIG{CHLD} = sub { my $sig=shift; if (defined $sth) { $sth->finish; } if (defined $stDst) { $stDst->finish; } if (defined $dbh ) { $dbh->disconnect; } if (defined $socket) { $socket->shutdown(2); } sleep(5); do { $pid = waitpid(-1,&WNOHANG); } until $pid == -1; log_notice ("Client : Caught signal SIG$sig\n"); if($sig eq 'PIPE') { log_notice ("Connection Ended\n"); } if($sig eq 'TERM' || $sig eq 'HUP' || $sig eq 'INT') { log_notice ("User Cancelling?\n"); exit 1; #User cancelling job ? } if ($connected) { $timestamp=time; if($timestamp < $midnight) { log_notice ("Client : Restarting $0\n"); log_error("End Processing $src_cdr_file\n"); exec '/home/user/$0' || log_warn ("Client : Could not exec $0\n"); exit 6; # Something is wrong if this exit is taken } # End if $timestamp log_notice ("Client : Normal Termination\n"); log_error("End Processing $src_cdr_file\n\n"); exec '/usr/bin/perl', '/home/user/$0' || log_warn ("Client : Could not exec $0\n"); exit 7; # Something is wrong if this exit is taken } # End if $connected }; # End anonymous sub

Replies are listed 'Best First'.
Re: IO::Socket what's this doing?
by jasonk (Parson) on Dec 26, 2007 at 19:50 UTC

    The subroutine is a signal handler indicating what the process should do when it receives a TERM signal (a signal sent to processes to indicate they should exit), an INT signal (a request to interrupt the process), a PIPE signal (the process attempted to write to a pipe that was not connected at the other end), or a CHLD signal (a child process created by this process exited). For more information on signal handling, look for %SIG in perlvar and at perlipc under 'Signals'.

    Definitions of those types of signals on wikipedia...


    We're not surrounded, we're in a target-rich environment!
Re: IO::Socket what's this doing?
by KurtSchwind (Chaplain) on Dec 27, 2007 at 12:55 UTC

    From reading the code it appears you are on a *nix system.

    If you are curious about the different signals you can send/receive you can type

    kill -l
    to get a list of all the commands that can be sent to a process (along with their numerical equivs).

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.