Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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?

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
Domain Nodelet?
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 rifling through the Monastery: (4)
As of 2021-10-22 07:14 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (85 votes). Check out past polls.