Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Forcing return from Net::SSH::Perl connection

by athomason (Curate)
on May 22, 2001 at 11:04 UTC ( #82177=perlquestion: print w/ replies, xml ) Need Help??
athomason has asked for the wisdom of the Perl Monks concerning the following question:


I'm using Net::SSH::Perl (thanks btrott!) in non-interactive mode to start up a daemon on another server. The daemon needs to stay alive after my connection dies, so I've been attempting to start up the process in the background. However, $ssh->cmd( "~/ &" )
hangs after executing the command. I thought I could trick the system with $ssh->cmd( "sh -c '~/ &'" )
but that hangs just the same. If the daemon (or any process, e.g. perl -e 'sleep 10') eventually exits on its own, the $ssh->cmd call returns and the programs returns. It seems that the login shell won't return until the process it spawned dies. This seems to generalize to other login shells as well (I tried rsh), but not non-login shells. My last resort would be an alarm to kill the connection, but that's painfully ugly. Does anyone know how to disable or work around this behavior within Net::SSH::Perl?

Comment on Forcing return from Net::SSH::Perl connection
Select or Download Code
Replies are listed 'Best First'.
(tye)Re: Forcing return from Net::SSH::Perl connection
by tye (Sage) on May 22, 2001 at 11:07 UTC

    The most common cause of this is that stdout and stderr are still open in the subprocess so reading the end of that/those "pipe(s)" doesn't get end-of-file until the subprocess dies (or closes stdout and stderr). So redirect stdout and sterr to a file: $ssh->cmd("~/ >startup.log 2>&1 &")

    If that still doesn't work, then you probably have an issue with controlling terminal and/or process groups so you'll probably have to "daemonize" the script (see "perldoc -q daemon").

            - tye (but my friends call me "Tye")
Re: Forcing return from Net::SSH::Perl connection
by busunsl (Vicar) on May 22, 2001 at 11:41 UTC
    Another solution could be the nohup command, which disconnects STD* from the calling shell:
    $ssh->cmd( "nohup ~/ &" )

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://82177]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2015-11-25 11:10 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (674 votes), past polls