Net::SSH - kick off background job


#1

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!


#2

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 &


#3

On Thu, Dec 18, 2008 at 2:27 AM, Brian C. removed_email_address@domain.invalid
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


#4

On Dec 18, 2008, at 9:26 AM, Douglas S. 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 B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#5

Douglas S. wrote:

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

… which nohup does anyway (according to its manpage, under Ubuntu
Hardy anyway)


#6

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.


#7

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.


#8

On Thu, Dec 18, 2008 at 6:59 AM, Brian C. removed_email_address@domain.invalid
wrote:

Douglas S. 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