in reply to Re: Run and kill external programm after x seconds
in thread Run and kill external programm after x seconds
If I add logic to the main program to exit the while loop before the subprogram has finished, the subprogram does not get terminated early.
use strict; use warnings; #1176332 my $cmd_1 = "perl 1176332p2.pl"; # (renamed from OP's hello.pl) open (CMD, "$cmd_1 2>&1|") or warn ("!!! Can't run program: $!\n"); my $start = time(); printf "Started at %s\n",$start; while (my $line = <CMD>) { print "->" . $line; if (time() > $start + 2) { printf "Stopping at %d!\n",time(); last; } if ($line =~ m/^xyz/) { print "\n Pretending to do some string manipulation\n"; } else { print "(from $0): Nothing yet | "; } } close CMD; printf "Actually stopped at %d\n", time();
H:\perl>perl 1176335.pl Started at 1479848254 ->Hello World from 1176332 PART 2 (from 1176335.pl): Nothing yet | ->Sleeping 10 seconds (from 1176335.pl): Nothing yet | ->. (from 1176335.pl): Nothing yet | ->. (from 1176335.pl): Nothing yet | ->. Stopping at 1479848257! Actually stopped at 1479848264
The main program does not return control to the command line until 7 seconds later, not until the subprogram has terminated on its own.
|
---|
In Section
Seekers of Perl Wisdom