On 10/15/05, zentara <[EMAIL PROTECTED]> wrote:
> On Sat, 15 Oct 2005 08:56:36 +0700, [EMAIL PROTECTED] (Beast) wrote:
>
> >
> >Hello,
> >
> >My perl script requires to invoke external program, however this
> >external program sometimes take forever to complete. The external
> >program accessing /dev/ircomm0 and whenever the IR client was hang or
> >out of range, this program try to search forever making the calling
> >program seems to hang.
> >Unfortunately, perl needs to be run unatended, scheduled using cron.
> >
> >My question is, how do I add a timer, so whenever external script did
> >not return results after x seconds just abort the program.
> >
> >snippet:
> >
> >----
> >my @results = `$sms_bin $options --msg="@sms_msg"
> >--number="$rcpt_number" 2>&1`;
> >---
>
> Search google and groups.google for "perl alarm".
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> foreach my $script (@scripts) {
>  eval {
>    alarm(360);      # set time limit to 6 mins
>    system($script);
>     alarm(0);        # if $script finishes in <6min reset alarm
>     }
>   if ($@) {
>    # do stuff if eval fails (eg timeout)
>     }
>

Maybe. But you can't bank on alarm DTRT with setting $! with system()
on it's own, and you want to know whether eval fails becuase of the
timeout or something else. See perldoc -f alarm for details. It's
better to trap SIGALRM when using alarm for external cals:

my $timeout = 60;
my $time;
eval {
    local $SIG{ALRM} = sub { die "timeout!" };
    alarm $timeout;
    system($script);
    $time = alarm 0;
}
if ($@) {
    if ($@ =~ /timeout/) {
        #do something
    }
    else {
        #do something else for other failures
    }
} else {
    print "External call finished in ".($timeout - $time)." seconds\n";
}

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to