>>>>> "Kelly" == Kelly Jones
<kelly.terry.jones gmail.com> writes:
Kelly> I want to use system() (or `command`) to run an
external command from
Kelly> my Perl script. However, if the external command
takes more than 30
Kelly> seconds (for example) to run, I want to kill it,
and move on with the
Kelly> rest of my Perl script. How do I do this?
You'll have to manage the forking yourself, and then have
the parent
kill the child if it doesn't finish within your time.
Something like:
my $result = eval {
my $kidpid = open my $kidhandle, "-|";
defined $kidpid or die "Cannot fork: $!";
if ($kidpid) { # I am the parent:
alarm(30);
local $ARGV = sub {
kill 15, $kidpid;
waitpid $kidpid;
die "timeout";
};
my $buf;
$buf .= $_ while <$kidhandle>;
alarm(0);
$buf;
} else { # I am the kid:
exec "whatever", "command",
"you", "want";
die "Cannot find whatever: $!";
}
}; die $ unless $ =~ /timeout/;
That's untested, but probably pretty close.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlyn stonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|