Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Forcing return from Net::SSH::Perl connection

by athomason (Curate)
on May 22, 2001 at 11:04 UTC ( [id://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?

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.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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-16 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found