Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

killing a program called with system() if it takes too long?

by silkworth (Initiate)
on Dec 18, 2008 at 16:47 UTC ( #731311=perlquestion: print w/replies, xml ) Need Help??
silkworth has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have a script which processes numerous files with an external program. the script itself just creates the input file list, and loops through them all with a for loop, calling this external program each time. however, sometimes this external program has an internal error (bad input file, etc) so the perl script can't/won't move on to the next file unless i manually kill (Ctrl+Alt+Del) the external program. When all is going well this external program only takes a few seconds, so I'm trying to find a way to make the script wait for a little longer than it should take, then kill this external program if its still running (ie it's most likely stuck). Any ideas? I've been reading up on system, exec, kill, and sleep, but I'm new to perl and really don't know what i doing. Thanks!
  • Comment on killing a program called with system() if it takes too long?

Replies are listed 'Best First'.
Re: killing a program called with system() if it takes too long?
by zentara (Archbishop) on Dec 18, 2008 at 16:55 UTC
    See system call with time out. There are other ways too, if you want to use an event loop, or thread for a timer, but alarm is usually what you are after. An old example:
    #!/usr/bin/perl use strict; use warnings; my $timeout = 180; my @array = ( "command 1 goes here", "command 2 goes here", "command 3 goes here", ); for (@array) { eval { local $SIG{ALRM} = sub { die "alarm\n"; }; alarm $timeout; system($_); alarm 0; }; } # abigail says # Don't do it with system directly, or you might generate zombies. # fork(), do an exec() in the child, and then, in an eval block, # setup an alarm handler that does a die(), set the alarm, # waitpid() for your child, cancel the alarm. Outside the eval # block, check for $@, if it indicates the alarm was triggered, # kill the child and waitpid() for it. # No doubt there are some race conditions left in, but it's a start.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: killing a program called with system() if it takes too long?
by xdg (Monsignor) on Dec 18, 2008 at 18:19 UTC
    ...unless i manually kill (Ctrl+Alt+Del) ...

    Are you on Windows? If so, run your external program with Win32::Job, which has timeout support built in.

    use Win32::Job; my $job = Win32::Job->new; # Run 'perl Makefile.PL' for 10 seconds $job->spawn($Config{perlpath}, "perl Makefile.PL"); $job->run(10);


    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: killing a program called with system() if it takes too long?
by n3toy (Hermit) on Dec 18, 2008 at 16:55 UTC
    What have you tried so far that didn't work the way you wanted?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://731311]
Approved by Corion
[1nickt]: What do you all think of experimental = refaliasing ? Anyone using it?
[1nickt]: \my %foo = { bar => 1 }; say $foo{bar};

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2017-11-17 20:32 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (272 votes). Check out past polls.