cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

Hmm, I'm fried, and seem to be suffering code blindness here. Does anyone have any idea why this hangs when running the job, rather than running the job in the background and exiting??? Authentication is not the issue, and I know the first script runs, because it's in the process list on the first machine. Trouble is, it doesn't iterate to the next machine.
my @hosts=( list of hosts here ); for (@hosts) { print "Running on $_...\n"; print `scp script.pl $_:~/`; print `ssh $_ "nohup nice perl script.pl &"`; }
Has anyone encountered anything similar? If I ssh in manually and run the command, it works ok, though I do need to hit enter twice on running to get back to the shell.

Hmmm, maybe I need to use Expect.

Ack. Any thoughts?

cLive ;-)

Update 2004-04-19: found the solution in the ssh FAQ:

print `ssh $_ "nohup nice perl script.pl < /dev/null > /dev/null 2>&1 +&"`;

Replies are listed 'Best First'.
Re: Slightly OT - automating nohup bg jobs...
by jonadab (Parson) on Jan 21, 2004 at 01:06 UTC

    Well, this isn't directly the question you asked, but you could probably work around the issue by having either script (or both) fork.


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
Re: Slightly OT - automating nohup bg jobs...
by bluto (Curate) on Jan 21, 2004 at 17:49 UTC
    I'm not sure why you are using backticks and trying to print the result since you are trying to background the job, but I'm not sure that matters.

    I'm no ssh guru, but I'm assuming ssh is keeping the session open since stdout and stderr are still open. If I redirect stdout and stderr, it seems to work ok for me on Linux. You may also want to redirect stdin as well. Try something like this...

    print `ssh myhost "nohup nice perl script.pl </dev/null >/dev/null 2>& +1 &"`;
    Another option might be to backround the ssh process on your local machine instead (though these will of course accumulate).

    bluto

      Thanks for input. FYI, the output gets appended to the nohup.out file by default and is useful to tail when testing (at least on FreeBSD). I'm not an expert on this at all - just hacks I found that seem to be useful.

      I really should learn bash in detail at some point...

      cLive ;-)

        I'm not sure that nohup is sufficient though to completely close stdout and stderr. I think the shell you spawn on the remote machine is itself holding a copy of these open.

        FWIW, I use zsh for my main shell, but almost always write scripts that are sh/ksh compatible since these are installed on almost every unix-like machine (and much more consistent than using csh).

        bluto