Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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:

Monks-

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( "~/startup.pl &" )
hangs after executing the command. I thought I could trick the system with $ssh->cmd( "sh -c '~/startup.pl &'" )
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
(tye)Re: Forcing return from Net::SSH::Perl connection
by tye (Cardinal) 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.pl >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 ~/startup.pl &" )

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2014-09-23 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls