Process hanging -SIGALRM not delivered- Perl -
i have command i'm executing using open pipe, , want set timeout of 10 seconds , have sub process aborted if execution time exceeds this. however, code causes program hang- why alarm not getting delivered properly?
my $pid = 0; $cmd = "somecommand"; print "running command # $num"; eval { local $sig{alrm} = sub { print "alarm \n"; kill 9, $pid; }; alarm 10; pid = open(my $fh, "$cmd|"); alarm 0; }; if($@) { die unless $@ eq "alarm \n"; } else { print $_ while(<$fh>); }
edit:
so answers below, have:
my $pid = open(my $fh, qq(perl -e 'alarm 10; exec \@argv; die "exec: $!\n" ' $cmd |)); print $_ while(<$fh>);
but print alarm clock console when alarm times out...whereas dont specify anywhere in code...how can rid of this, , put custom alarm event handler?
thanks!
i want set timeout of 10seconds , have sub process aborted if execution time exceeds this
a different approach set alarm on subprocess itself, handy scripting language have:
my $cmd = "somecommand"; $pid = open(my $child_stdout, '-|', 'perl', '-e', 'alarm 10; exec @argv; die "exec: $!"', $cmd); ...
your child process perl (well, shell , perl), set alarm on , exec (replace with) $somecommand
. pending alarms, however, inherited across exec()
s.
Comments
Post a Comment