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 MichiIn reply to Re^2: Run and kill external programm after x seconds
by demichi
in thread Run and kill external programm after x seconds
by demichi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |