Forum: Ruby Net::SSH - kick off background job

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3da512875f3295be2d577fa70b3493b4?d=identicon&s=25 Sebastian W. (globulus)
on 2008-12-18 00:57
Does anyone know of a way to tell Net::SSH to start running a script but
then effectively "disown" that process?

I have a long-running job I'd like to kick off using Net::SSH but I
don't want the connection to persist for the entire length of the job. I
just want to kick off the job, let it start running and then basically
not care what happens. Of course I care, but I have a different process
that will actually pick up whether or not the long-running task was
successful or not.

Thanks much!
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-12-18 11:35
Sebastian W. wrote:
> Does anyone know of a way to tell Net::SSH to start running a script but
> then effectively "disown" that process?
>
> I have a long-running job I'd like to kick off using Net::SSH but I
> don't want the connection to persist for the entire length of the job. I
> just want to kick off the job, let it start running and then basically
> not care what happens. Of course I care, but I have a different process
> that will actually pick up whether or not the long-running task was
> successful or not.

nohup somecommand someargs &
7cee9dd780723c5fa0f06a242338e57c?d=identicon&s=25 Douglas Seifert (Guest)
on 2008-12-18 15:35
(Received via mailing list)
On Thu, Dec 18, 2008 at 2:27 AM, Brian Candler <b.candler@pobox.com>
wrote:

>
> nohup somecommand someargs &
> --
> Posted via http://www.ruby-forum.com/.
>
>
You probably need something like this:
nohup somecommand someargs > /tmp/out.log < /dev/null &

the "< /dev/null" lets you detach the job from the terminal.

Cheers,
Doug Seifert
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-12-18 16:02
(Received via mailing list)
On Dec 18, 2008, at 9:26 AM, Douglas Seifert wrote:
>>> just want to kick off the job, let it start running and then
>
> the "< /dev/null" lets you detach the job from the terminal.
>
> Cheers,
> Doug Seifert


If your platform has setsid (set session id), that is a better option
for something like this.

setsid somecommand someargs > /tmp/out.log 2>&1

Note the use of 2>&1 to redirect stderr(2) to(>) the same(&) place as
stdout(1).  The order is important, redirect stdout first.

Also note that you do not put this command in the background with &
like in the nohup case.  A new process is forked by setsid  so that
the session id can be set and the controlling terminal disassociated
from the process.

-Rob

P.S. Of course, this isn't Net::SSH specific, but applies to any Linux
system.

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-12-18 16:07
Douglas Seifert wrote:
> the "< /dev/null" lets you detach the job from the terminal.

... which nohup does anyway (according to its manpage, under Ubuntu
Hardy anyway)
3da512875f3295be2d577fa70b3493b4?d=identicon&s=25 Sebastian W. (globulus)
on 2008-12-19 09:33
Cool, thank you very much! I haven't had a chance to try this out yet,
but I will give it a shot.

The issue I ran into with just regular old "nohup command &" is that,
while it works just as you'd expect if you're ssh'ed onto a box and
doing stuff, it doesn't work so well if you're just trying to send one
command in and be done.

E.g. usually you might be able to do something like 'ssh me@box "cat
somefile.txt"' but my colleague pointed out that to "disown" the process
I'd need to do something like

ssh me@box "nohup command" & -- it's the ampersand outside the quotes
that I wasn't sure how to deal with.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-12-19 10:02
Sebastian W. wrote:
> E.g. usually you might be able to do something like 'ssh me@box "cat
> somefile.txt"' but my colleague pointed out that to "disown" the process
> I'd need to do something like
>
> ssh me@box "nohup command" & -- it's the ampersand outside the quotes
> that I wasn't sure how to deal with.

But I thought you were using Net::SSH? In that case, the ssh me@box part
is not used.

Anyway, you want the ampersand *inside* the quotes if you want the
"nohup command" to return immediately, so that you can logout from the
remote system while the remote command is running.
7cee9dd780723c5fa0f06a242338e57c?d=identicon&s=25 Douglas Seifert (Guest)
on 2008-12-20 23:08
(Received via mailing list)
On Thu, Dec 18, 2008 at 6:59 AM, Brian Candler <b.candler@pobox.com>
wrote:

> Douglas Seifert wrote:
> > the "< /dev/null" lets you detach the job from the terminal.
>
> ... which nohup does anyway (according to its manpage, under Ubuntu
> Hardy anyway)
> --
> Posted via http://www.ruby-forum.com/.
>
> On Red Hat EL 5 it does not.  On newer distros it does.

-Doug Seifert
This topic is locked and can not be replied to.