Net::SSH - kick off background job

S

Sebastian W.

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!
 
B

Brian Candler

Sebastian said:
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 &
 
D

Douglas Seifert

[Note: parts of this message were removed to make it a legal post.]

nohup somecommand someargs &
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
 
R

Rob Biedenharn

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


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
(e-mail address removed)
 
B

Brian Candler

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

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

Sebastian W.

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.
 
B

Brian Candler

Sebastian said:
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.
 
D

Douglas Seifert

[Note: parts of this message were removed to make it a legal post.]

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

-Doug Seifert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top