Net::SSH - kick off background job

Discussion in 'Ruby' started by Sebastian W., Dec 17, 2008.

  1. Sebastian W.

    Sebastian W. Guest

    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!
    --
    Posted via http://www.ruby-forum.com/.
    Sebastian W., Dec 17, 2008
    #1
    1. Advertising

  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 &
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Dec 18, 2008
    #2
    1. Advertising

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

    On Thu, Dec 18, 2008 at 2:27 AM, Brian Candler <> wrote:

    > 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 &
    > --
    > 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
    Douglas Seifert, Dec 18, 2008
    #3
  4. On Dec 18, 2008, at 9:26 AM, Douglas Seifert wrote:
    > On Thu, Dec 18, 2008 at 2:27 AM, Brian Candler <>
    > wrote:
    >> 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 &
    >>

    > 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
    Rob Biedenharn, Dec 18, 2008
    #4
  5. 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/.
    Brian Candler, Dec 18, 2008
    #5
  6. Sebastian W.

    Sebastian W. Guest

    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.



    --
    Posted via http://www.ruby-forum.com/.
    Sebastian W., Dec 19, 2008
    #6
  7. 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.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Dec 19, 2008
    #7
  8. [Note: parts of this message were removed to make it a legal post.]

    On Thu, Dec 18, 2008 at 6:59 AM, Brian Candler <> 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
    Douglas Seifert, Dec 20, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. greg
    Replies:
    2
    Views:
    704
    Scott M.
    Aug 27, 2004
  2. Carl Johansen
    Replies:
    3
    Views:
    926
    Carl Johansen
    Jul 3, 2005
  3. rodchar

    kick off batch sproc

    rodchar, Aug 1, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    244
    rodchar
    Aug 1, 2008
  4. loial
    Replies:
    2
    Views:
    287
    Nobody
    Jul 20, 2010
  5. Carl Johansen
    Replies:
    1
    Views:
    145
    Bob Barrows [MVP]
    Jul 2, 2005
Loading...

Share This Page