D
dn_perl
I am trying to timeout an external program, and if it is timed
out, I want to kill it.
I have used the following code :
#!/usr/local/bin/perl
use strict ;
my $timeout = 6 ;
my $my_pid ;
eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm $timeout;
system("/mydir/my_program") ; # stmt 1
# $my_pid = open EXTRACT , " /mydir/my_program | " ; # stmt 2
alarm 0;
};
if ($@) {
die unless $@ eq "alarm\n"; # propagate unexpected errors
print "killing process\n" ;
# timed out
}
else {
# didn't time out
}
print "my_pid is $my_pid Z\n" ;
-----------
my_program runs for 20 seconds. So it is always timed out. But
I want to kill it before that.
The problem is : if I use "system" command to start the external
program, I pass if($@) condition and can do some processing.
However I do not know PID of my_program in that case.
But if I comment out "system" cal and use stmt 2 instead to
start the process and get its PID, I can't pass if($@) condition.
(And when I run the command : open EXTRACT , " /mydir/my_program | "
what is the program's output geting piped *to* ??)
Please advise how I should get around this problem.
Thanks in advance.
out, I want to kill it.
I have used the following code :
#!/usr/local/bin/perl
use strict ;
my $timeout = 6 ;
my $my_pid ;
eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm $timeout;
system("/mydir/my_program") ; # stmt 1
# $my_pid = open EXTRACT , " /mydir/my_program | " ; # stmt 2
alarm 0;
};
if ($@) {
die unless $@ eq "alarm\n"; # propagate unexpected errors
print "killing process\n" ;
# timed out
}
else {
# didn't time out
}
print "my_pid is $my_pid Z\n" ;
-----------
my_program runs for 20 seconds. So it is always timed out. But
I want to kill it before that.
The problem is : if I use "system" command to start the external
program, I pass if($@) condition and can do some processing.
However I do not know PID of my_program in that case.
But if I comment out "system" cal and use stmt 2 instead to
start the process and get its PID, I can't pass if($@) condition.
(And when I run the command : open EXTRACT , " /mydir/my_program | "
what is the program's output geting piped *to* ??)
Please advise how I should get around this problem.
Thanks in advance.