List Info

Thread: system() command with a time limit




system() command with a time limit
user name
2006-12-25 01:45:11
I want to use system() (or `command`) to run an external
command from
my Perl script. However, if the external command takes more
than 30
seconds (for example) to run, I want to kill it, and move on
with the
rest of my Perl script. How do I do this?

-- 
We're just a Bunch Of Regular Guys, a collective group
that's trying
to understand and assimilate technology. We feel that
resistance to
new ideas and technology is unwise and ultimately futile.


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/
 
system() command with a time limit
user name
2006-12-25 02:59:10
>>>>> "Kelly" == Kelly Jones
<kelly.terry.jonesgmail.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
<merlynstonehenge.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/
 
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )