Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Run and kill external programm after x seconds

by ww (Archbishop)
on Nov 22, 2016 at 13:55 UTC ( [id://1176335]=note: print w/replies, xml ) Need Help??


in reply to Run and kill external programm after x seconds

Would this approach be a helpful alternative? (I ask because I'm not sure I entirely understand your real problem space.)

use strict; use warnings; #1176332 my $cmd_1 = "perl D:/_scratch/1176332p2.pl"; # (renamed from OP's hel +lo.pl) open (CMD, "$cmd_1 2>&1|") or warn ("!!! Can't run program: $!\n"); while (my $line = <CMD>) { print $line; if ($line =~ m/^xyz/) { print "\n Pretending to do some string manipulation\n"; } else { print "(from $0): Nothing yet | "; } } close CMD;
use strict; use warnings; # D:/_scratch/1176332p2.pl $| = 1; print "Hello World from 1176332 PART 2\n"; print "Sleeping 10 seconds \n"; for (1 .. 10) { sleep 1; print ". \n"; } print "xyz \n";

Execution:

D:\_scratch>D:\_scratch\1176332.pl Hello World from 1176332 PART 2 (from D:\_scratch\1176332.pl): Nothing yet | Sleeping 10 seconds (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | . (from D:\_scratch\1176332.pl): Nothing yet | xyz Pretending to do some string manipulation D:\_scratch>

If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

check Ln42!

Replies are listed 'Best First'.
Re^2: Run and kill external programm after x seconds
by GotToBTru (Prior) on Nov 22, 2016 at 21:03 UTC

    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.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re^2: Run and kill external programm after x seconds
by demichi (Beadle) on Nov 22, 2016 at 14:13 UTC
    Hi,

    my problem is that I run an external program X (I do not have control of this program X) and I get output from this program X. This program X may dies or don't give answer for hours.

    I would like to wait for e.g. 60 seconds and if there is no new input from this program X it should be closed/killed and my perl program should go on with next steps.

    kind regards, de michi

      So would it not work to reverse the logic of the m/.../ or -- at the 60 second mark -- to simply test for a void as below?

      D:\_scratch>perl -e "my $foo=''; if ($foo =~//){ print 'found nothing' +;} exit();" found nothing D:\_scratch>

      ++$anecdote ne $data

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176335]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-28 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found