in reply to Re^7: How to timeout if a call to an external program takes too long
in thread How to timeout if a call to an external program takes too long

So, as examples of "How to timeout a call to an external program" go, the first one is completely useless as it does nothing except print a message via a 'longcall'.

And the second one, whilst returning control to the calling program, leaves the spawned program running (or hung) with no possible mechanism to terminate it.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re^8: How to timeout if a call to an external program takes too long

Replies are listed 'Best First'.
Re^9: How to timeout if a call to an external program takes too long
by kyle (Abbot) on Aug 30, 2007 at 14:47 UTC

    Well, yes.

    If you're worried about terminating the spawned program, don't use backticks. Most of the time, I don't use backticks anyway. I'd much rather do something like this:

    my $cmd_pid = open my $cmd_fh, '-|', $cmd or die "Can't run '$cmd': $!\n"; my $yawn = do { local $/; <$cmd_fh> }; close $cmd_fh;

    It does the same thing, and it complains about errors, and it gives me the PID of the child process, which I can use to cause the sort of interruption you're looking for:

    my $start_time = time; sub exec_time { return time - $start_time } my $cmd = 'sleep 3600 ; echo "yawn"'; my $yawn; my $cmd_pid = open my $cmd_fh, '-|', $cmd or die "Can't run '$cmd': $!\n"; eval { local $SIG{ALRM} = sub { kill 15, $cmd_pid; die "alarm\n" }; alarm 5; $yawn = do { local $/; <$cmd_fh> }; alarm 0; }; close $cmd_fh; printf "(%d) yawn? $yawn", exec_time(); __END__ (5) yawn?

    Note that in this case, if I don't kill the child, the program will still hang when it gets to close, because that will wait for the pipe to finish. You can put the close inside the eval, but then you have a filehandle dangling open.

    Note also that if the child spawns a child (as it does in this case because the child is sh -c), then that grandchild is not killed. I don't see that there's much to do about that in any case.

    Also, just for the record, this morning I'm testing on a different machine.

    kyle@xxx:~$ perl -v | head -5 This is perl, v5.8.8 built for i486-linux-gnu-thread-multi Copyright 1987-2006, Larry Wall kyle@xxx:~$ uname -a Linux xxx 2.6.20-16-generic #2 SMP Thu Jun 7 20:19:32 UTC 2007 i686 GN +U/Linux