alarm($timeout) timing out only one iteration - Solaris9

P

peco

Hello,
I have the following code:
eval
{
local $SIG{ALRM} = sub { die ;};
alarm(3);
while (<CMD>)
{
$oldvar= $_;
}
alarm(0);
};

CMD is an open pipe that ssh-es to a list of servers and retrieves a
value. If ssh connection takes too long the intention is to kill it and
proceed with the rest on the list. The problem is that alarm works only
on the first slow connection. Subsequent iterations that take longer
than 3 seconds do not time out. I have found several posts regarding
this but nothing has worked so far. I understand this is architecture
dependant implementation but so far I am unable to find the right
incantation for Solaris 9.

Thanks!
 
C

Charles DeRykus

Hello,
I have the following code:
eval
{
local $SIG{ALRM} = sub { die ;};
alarm(3);
while (<CMD>)
{
$oldvar= $_;
}
alarm(0);
};

CMD is an open pipe that ssh-es to a list of servers and retrieves a
value. If ssh connection takes too long the intention is to kill it and
proceed with the rest on the list. The problem is that alarm works only
on the first slow connection. Subsequent iterations that take longer
than 3 seconds do not time out. I have found several posts regarding
this but nothing has worked so far. I understand this is architecture
dependant implementation but so far I am unable to find the right
incantation for Solaris 9.

More code needed to see what's going on... show how
the pipe is actually opened for instance.

Just a guess really but is the alarm(3) being reset
somewhere after there's a timeout...
 
A

Anno Siegel

peco said:
Hello,
I have the following code:
eval
{
local $SIG{ALRM} = sub { die ;};
alarm(3);
while (<CMD>)
{
$oldvar= $_;
}
alarm(0);
};

CMD is an open pipe that ssh-es to a list of servers and retrieves a
value. If ssh connection takes too long the intention is to kill it and
proceed with the rest on the list. The problem is that alarm works only
on the first slow connection. Subsequent iterations that take longer
than 3 seconds do not time out.

The alarm you set times out exactly once, breaking the while loop.
It can't time out more than one event.

Your description of what CMD does is more than vague. It would have
been your job to develop some demo job that everyone can run and watch
the result.

Assuming that it prints exactly one line for every "step", moving
alarm( 3) into the loop would help. As in

my $prog = <<'EOP';
$| = 1;
for ( 1 .. 10 ) {
my $t = 1 + int rand( 5);
print "see ya in $t secs\n";
sleep $t;
}
EOP
open CMD, '-|', "perl -e '$cmd'";

eval
{
local $SIG{ALRM} = sub { die ;};
while (<CMD>)
{
alarm(3);
print;
}
alarm(0);
};

This times out the first time CMD fails to deliver the next line
within 3 seconds. Is that what you want?

Anno
 
B

Brian Wakem

Anno said:
The alarm you set times out exactly once, breaking the while loop.
It can't time out more than one event.

Your description of what CMD does is more than vague. It would have
been your job to develop some demo job that everyone can run and watch
the result.

Assuming that it prints exactly one line for every "step", moving
alarm( 3) into the loop would help. As in

my $prog = <<'EOP';
$| = 1;
for ( 1 .. 10 ) {
my $t = 1 + int rand( 5);
print "see ya in $t secs\n";
sleep $t;
}
EOP
open CMD, '-|', "perl -e '$cmd'";


Global symbol "$cmd" requires explicit package name at....

I pressume that should be $prog.
 
P

peco

Thank you for your help. Here is the entire routine:

foreach $host (list_hosts())
{
my $oldpass="";
my $cmd = "su - user000 -c \".
/user/home/.ssh-agent-serverXYZ;ssh $host cat /etc/passwd\"";
my $pid = open(CMD, "$cmd |");
print "Process ID of open pipe - $pid\n";
eval
{
local $SIG{ALRM} = sub { print "TIMEOUT after $timeout
seconds\n";die ;};
alarm(3);
while (<CMD>)
{
$oldpass= $_;
}
alarm(0);
};
kill 9,$pid;
close(CMD);

$oldpass =~ s/\n//;
print $host."---".$oldpass."\n";

}

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top