Adrian Blanco Adrian Blanco - 2 years ago 134
Perl Question

Perl TIMEOUT output message

I'm programming a perl script to monitorize a DB with Nagios.
I'm using alarm function from Time::HiRes library for the timeout.

use Time::HiRes qw[ time alarm ];
alarm $timeout;

Everything works fine. The thing is I want to change the output message cause it returns "Temporizador" and if I do an

echo $?

Returns 142. I want to change the message in order to make an "exit 3" so it can be recognized by Nagios.

Already tried 'eval' but doesn't work.

Answer Source

You should handle the ALRM signal. For example:

#!/usr/bin/env perl
use strict;
use warnings;
use Time::HiRes qw[ time alarm ];

$SIG{ALRM} = sub {print "Custom message\n"; exit 3};

alarm 2;
sleep 10; # this line represents the rest of your program, don't include it

This will output:

18:08:20-eballes@urth:~/$ ./ 
Custom message
18:08:23-eballes@urth:~/$ echo $?

For an extended explanation about handling signals check this nice tutorial on perltricks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download