Search code examples
perlsleepsigkill

Perl script is getting killed during sleep()


I have a quite simple perl script, that in one function does the following:

    if ( legato_is_up() ) {
        write_log("INFO:        Legato is up and running. Continue the installation.");
        $wait_minutes = $WAITPERIOD + 1;
        $legato_up = 1;
    }
    else {
        my $towait = $WAITPERIOD - $wait_minutes;
        write_log("INFO:        Legato is not up yet. Waiting for another $towait minutes...");
        sleep 30;
        $wait_minutes = $wait_minutes + 0.5;
    }

For some reason, sometimes (like 1 in 3 runs) the script gets killed. I don't know who's responsible for the kill, I just know it happens during the "sleep" call.

Can anyone give me a hint here? After script is killed, it's job is not done, which is a big problem.


Solution

  • Without knowing what else is running on your system, it's anybody's guess. You could add a signal handler, but all that it would tell you is which signal it was (and when), but not who sent it:

    foreach my $signal (qw(INT PIPE HUP))
    {
        my $old_handler = $SIG{$signal};
        $SIG{$signal} = sub {
            print time, ": ", $signal, " received!\n";
            $old_handler->(@_) if $old_handler;
        };
    }
    

    You also may want to consider adding a WARN and DIE handler, if you are not logging output from stderr.