Catch Timeout for system calls in Windows?

P

peterkayatwork

I'm using ActiveState Perl on a Windows XP platform, and want to kick
off some scripts from one central location. However, I want to make
sure these scripts exit, and would like to timeout if they don't.
Ideally, I want to grab the output as well, but I can handle that by
redirecting to output files if I need.

Can anyone suggest a way to do this? Can anyone suggest what to look
for?

If I were doing this in a *nix environment, I'd try signals, but I
suspect that cygwin just isn't going to handle them; if anyone knows
better, let me know :)

Example of what won't work:
foreach my $script (@scripts) {
system($script); # no timeout!
}

Thanks,

--Peter
 
C

Chris Cole

I'm using ActiveState Perl on a Windows XP platform, and want to kick
off some scripts from one central location. However, I want to make
sure these scripts exit, and would like to timeout if they don't.
Ideally, I want to grab the output as well, but I can handle that by
redirecting to output files if I need.

Can anyone suggest a way to do this? Can anyone suggest what to look
for?

If I were doing this in a *nix environment, I'd try signals, but I
suspect that cygwin just isn't going to handle them; if anyone knows
better, let me know :)

Example of what won't work:
foreach my $script (@scripts) {
system($script); # no timeout!
}
How about using an eval block with an alarm? Someething like:

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)
}

Chris.
 
P

peterkayatwork

I got "Alarm clock" on the cygwin prompt and the script died.

I played around with it and came up with:

eval {
# from http://www.rocketaware.com/perl/perlfunc/alarm.htm
local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n required
alarm(5); # set time limit to 5 sec
system($script);
alarm(0); # if $script finishes in <5sec reset alarm
};
die if $@ && $@ ne "alarm\n"; # propagate errors
if ( $@ ) {
print "Timeout! [$@]\n";
# do stuff if eval fails (eg timeout)
}

Thanks for the suggestion :)

--Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top