Click here to get back home

fork and hanging

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
fork and hanging nazrat 01-30-2008
Posted by nazrat on January 30, 2008, 2:55 pm
Please log in for more thread options
I have been searching for the problem on the net but unable to get a
good answer so I'd like to post it here for help.

I have attempted to simultaneously ssh to a number of boxes (with
openssh 3.5p1 - 3.9p1) via forking subprocesses. However, there always
seem to be a few ssh sessions got stuck and thus, my control (parent)
process cannot exit. I have searched for similar problems regarding
ssh hanging with fork, but most results indicated that the issue was
on the remote machine with an inappropriately demonized process (ie.
not redirect 3 input streams). In my scenerio, the forking doesn't
happen on remote site and the remote command is just a simple one
(uname) as demonstrated in the scripts enclosed below.

In this setting, I have an 'expect' script (spawn.exp) used to cache
the password and feed it to spawned processes (ssh.pl). I am not sure
if there is any animosity between Perl fork and ssh that I am not
aware of?

% ./spawn.exp ./ssh.pl

ssh.pl:

#!/usr/local/bin/perl -w
use strict;

my %pids;

for my $id ('01'..'05') {
my $pid = fork();
die "fork $id: $!\n" unless defined $pid;
if ($pid == 0) {
#open STDIN, '</dev/null';
#open STDOUT, '>/dev/null';
#open STDERR, '>/dev/null';
exec 'ssh', "build-$id", 'uname', '-a';
}
else {
$pids = "build-$id";
}
}
while (%pids) {
my $pid = wait();
last if $pid == -1;
delete $pids if exists $pids;
}

__END__


spawn.exp:

#!/usr/bin/expect --

set passwd ""
set timeout -1

eval spawn -noecho $argv
expect {
"Please try again." {
set passwd
exp_continue
}
"password:" {
if {$passwd == ""} {
stty -echo
send_user " (script) "
expect_user -re "(.*)\n"
stty echo
set passwd $expect_out(1,string)
} else {
send_user " (supplied by script) "
}
send -- "$passwd\r"
exp_continue
}
}

Posted by xhoster on January 30, 2008, 3:55 pm
Please log in for more thread options
>
> In this setting, I have an 'expect' script (spawn.exp) used to cache
> the password and feed it to spawned processes (ssh.pl). I am not sure
> if there is any animosity between Perl fork and ssh that I am not
> aware of?
>
> % ./spawn.exp ./ssh.pl

I think I'd try having ssh.pl call spawn.exp, not the other way around.
If a single expect is trying to talk to many different processes over
the same file handles at the same time, I wouldn't be surprised if things
often go awry.


...

> exec 'ssh', "build-$id", 'uname', '-a';

exec "./spawn.exp ssh build-$id uname -a";

Or something like that, I don't use expect.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Similar ThreadsPosted
Net::SSH::W32Perl - hanging March 16, 2007, 11:09 pm
Perl debug hanging September 6, 2005, 1:07 pm
Hanging at several places when multiple-commanding Net::SSH::W32Perl October 17, 2006, 5:55 am
handling hanging database connections: timeout in perl November 24, 2006, 8:06 am
Problems with IO::socket hanging on windows (works fine on Unix) June 2, 2005, 3:33 am
Fork Example July 5, 2007, 12:21 am
Using fork() November 13, 2007, 11:15 am
Fork within thread? August 4, 2004, 2:45 am
END blocks and fork March 1, 2005, 2:23 pm
fork() slowness. August 16, 2005, 2:48 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap