Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Run and kill external programm after x seconds

by demichi (Beadle)
on Nov 22, 2016 at 15:19 UTC ( [id://1176344]=note: print w/replies, xml ) Need Help??


in reply to Re: Run and kill external programm after x seconds
in thread Run and kill external programm after x seconds

Hi,

thanks that works (with system() instead of backticks - for any reason I do not get dir output with backticks.

I tried it with the CMD I would use and it does not stop after 2 seconds.

use warnings; use strict; my $time = 2; my $cmd = 'dir'; my $check = 1; my $output; print "check is $check\n"; eval { local $SIG{ALRM} = sub { die 'timed out'; }; alarm $time; my $cmd_1 = "perl hello.pl"; # see hello.pl code below" open (CMD, "$cmd_1 2>&1|") or warn ("!!! Can't run program +: $!\n"); while (my $line = <CMD>) { print $line; } close CMD; sleep 4; $check++; alarm 0; }; if ($@ && $@ =~ /timed out/){ print "damn command timed out!\n"; } else { print "$output\n"; } print "check is now $check\n";

hello.pl

use strict; use warnings; $| = 1; print "Hello World\n"; print "Sleeping 1000 seconds\n"; for (1 .. 1000) { sleep 1; print ".\n"; }

Any idea why this does not work?

rgds de Michi

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-26 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found