[Q] how can I start a shell process and return immediately?

Discussion in 'Ruby' started by Stephen Bannasch, Sep 29, 2007.

  1. I want to start a Java program from a Ruby program and have the Java
    program run in another process and have control returned to my Ruby
    program as soon as the Java process starts up successfully.

    I can do this from a shell by suffixing an '&':

    java -cp <classpath> <main_class>&

    starts the Java program in a background process thread and returns
    control the shell.

    When I try this from a Ruby program the program blocks until the Java
    program is terminated.
    Stephen Bannasch, Sep 29, 2007
    #1
    1. Advertising

  2. Stephen Bannasch

    7stud -- Guest

    Re: how can I start a shell process and return immediately?

    Stephen Bannasch wrote:
    >
    > When I try this from a Ruby program the program blocks until the Java
    > program is terminated.
    >


    "this"? Here let's play a little game. You try to help me. Here is
    my question: I have a for loop that doesn't work. Can you fix it?

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Sep 29, 2007
    #2
    1. Advertising

  3. Stephen Bannasch wrote:
    > java -cp <classpath> <main_class>&
    >
    > starts the Java program in a background process thread and returns
    > control the shell.
    >
    > When I try this from a Ruby program the program blocks until the Java
    > program is terminated.


    I don't know what exactly "this" is, but system("some_command &") works just
    fine here.


    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Sep 29, 2007
    #3
  4. Stephen Bannasch

    7stud -- Guest

    Re: how can I start a shell process and return immediately?

    r8test.rb
    -------
    sleep(4)
    puts "Data from subprocess: 10 20 30"



    main_program.rb
    --------------
    puts "main program"
    sleep(1)

    pipe = IO.popen("ruby r8test.rb", "w+")

    puts "main program executing while subprocess is sleeping"
    Process.wait

    pipe.close_write
    puts pipe.gets

    pipe.close_read
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Sep 29, 2007
    #4
  5. Stephen Bannasch

    7stud -- Guest

    Re: how can I start a shell process and return immediately?

    Stephen Bannasch wrote:
    >
    > * Thanks 7stud,
    >
    > IO.popen is interesting, I'm playing with your simple test programs and
    > reading chapter 11 in the Pickaxe book.


    You can consider the sleep(4) statement in the subprocess as a stand in
    for some calculation or task that takes 4 seconds to execute.

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Sep 30, 2007
    #5
  6. Stephen Bannasch

    Enling Li Guest

    Re: how can I start a shell process and return immediately?

    I have another quetion related to fire off a back ground shell process
    on a remote Linux.

    require "net/ssh"

    ssh = Net::SSH.start( '10.0.77.87', 'toor', :password=>'logapp')
    ssh.exec!("/root/test.pl &")
    ssh.close
    puts "I want to be here immediately, but not"

    The above code hangs also because test.pl contains a long sleep and do
    nothing. Can someone offer me a help?

    Thank you in advance.

    Enling

    Stephen Bannasch wrote:
    > I want to start a Java program from a Ruby program and have the Java
    > program run in another process and have control returned to my Ruby
    > program as soon as the Java process starts up successfully.
    >
    > I can do this from a shell by suffixing an '&':
    >
    > java -cp <classpath> <main_class>&
    >
    > starts the Java program in a background process thread and returns
    > control the shell.
    >
    > When I try this from a Ruby program the program blocks until the Java
    > program is terminated.


    --
    Posted via http://www.ruby-forum.com/.
    Enling Li, Jul 9, 2009
    #6
  7. Re: how can I start a shell process and return immediately?

    On Jul 9, 2009, at 5:13 PM, Enling Li wrote:

    > I have another quetion related to fire off a back ground shell process
    > on a remote Linux.
    >
    > require "net/ssh"
    >
    > ssh = Net::SSH.start( '10.0.77.87', 'toor', :password=>'logapp')
    > ssh.exec!("/root/test.pl &")
    > ssh.close
    > puts "I want to be here immediately, but not"
    >
    > The above code hangs also because test.pl contains a long sleep and do
    > nothing. Can someone offer me a help?
    >
    > Thank you in advance.
    >
    > Enling
    >
    > Stephen Bannasch wrote:
    >> I want to start a Java program from a Ruby program and have the Java
    >> program run in another process and have control returned to my Ruby
    >> program as soon as the Java process starts up successfully.
    >>
    >> I can do this from a shell by suffixing an '&':
    >>
    >> java -cp <classpath> <main_class>&
    >>
    >> starts the Java program in a background process thread and returns
    >> control the shell.
    >>
    >> When I try this from a Ruby program the program blocks until the Java
    >> program is terminated.


    If your system has the `setsid` (set session id) program (and the
    underlying system call), then you could try:

    ssh.exec!("setsid /root/test.pl")

    or

    setsid java -cp YOURCLASSPATH MainClass

    but you may wish to redirect output.

    ssh.exec!("setsid /root/test.pl >/tmp/test.log 2>&1")

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Jul 9, 2009
    #7
  8. Stephen Bannasch

    Enling Li Guest

    Re: how can I start a shell process and return immediately?

    Thank you, Rob for the quick reply and help. I tried the commmand you
    enclosed in the mail. It did not work out. I still got hang at the
    exec() command.

    Here are the options I did. They behave the same. They don't work. By
    the way, my linux system has setsid().

    ssh.exec!("setsid /root/test.pl >/tmp/test.log 2>&1")
    ssh.exec!("setsid /root/test.pl > /dev/null 2>&1")
    ssh.exec!("setsid /root/test.pl")

    Thanks.

    Enling


    Rob Biedenharn wrote:
    > On Jul 9, 2009, at 5:13 PM, Enling Li wrote:
    >
    >> The above code hangs also because test.pl contains a long sleep and do
    >>>
    >>> I can do this from a shell by suffixing an '&':
    >>>
    >>> java -cp <classpath> <main_class>&
    >>>
    >>> starts the Java program in a background process thread and returns
    >>> control the shell.
    >>>
    >>> When I try this from a Ruby program the program blocks until the Java
    >>> program is terminated.

    >
    > If your system has the `setsid` (set session id) program (and the
    > underlying system call), then you could try:
    >
    > ssh.exec!("setsid /root/test.pl")
    >
    > or
    >
    > setsid java -cp YOURCLASSPATH MainClass
    >
    > but you may wish to redirect output.
    >
    > ssh.exec!("setsid /root/test.pl >/tmp/test.log 2>&1")
    >
    > -Rob
    >
    > Rob Biedenharn http://agileconsultingllc.com
    >


    --
    Posted via http://www.ruby-forum.com/.
    Enling Li, Jul 10, 2009
    #8
  9. Re: how can I start a shell process and return immediately?

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

    On Thu, Jul 9, 2009 at 4:53 PM, Enling Li <> wrote:

    > Thank you, Rob for the quick reply and help. I tried the commmand you
    > enclosed in the mail. It did not work out. I still got hang at the
    > exec() command.
    >
    > Here are the options I did. They behave the same. They don't work. By
    > the way, my linux system has setsid().
    >
    > ssh.exec!("setsid /root/test.pl >/tmp/test.log 2>&1")
    > ssh.exec!("setsid /root/test.pl > /dev/null 2>&1")
    > ssh.exec!("setsid /root/test.pl")
    >


    Would just dropping the ssh section into it's own thread accomplish your
    task? That seems like a rather simple way of getting what you want.

    John
    John W Higgins, Jul 10, 2009
    #9
  10. Stephen Bannasch

    Enling Li Guest

    Re: how can I start a shell process and return immediately?

    I tried that before. The thread won't solve the issue because once child
    thread established it does the same way as main tread. When the main
    thread terminates, the child gets killed also. It ends up nothing
    happened to the shell command.

    Thanks.

    Enling

    John W Higgins wrote:
    > On Thu, Jul 9, 2009 at 4:53 PM, Enling Li <>
    > wrote:
    >
    >>

    > Would just dropping the ssh section into it's own thread accomplish your
    > task? That seems like a rather simple way of getting what you want.
    >
    > John


    --
    Posted via http://www.ruby-forum.com/.
    Enling Li, Jul 10, 2009
    #10
  11. Re: how can I start a shell process and return immediately?

    On Jul 9, 2009, at 7:53 PM, Enling Li wrote:

    > Thank you, Rob for the quick reply and help. I tried the commmand you
    > enclosed in the mail. It did not work out. I still got hang at the
    > exec() command.
    >
    > Here are the options I did. They behave the same. They don't work. By
    > the way, my linux system has setsid().
    >
    > ssh.exec!("setsid /root/test.pl >/tmp/test.log 2>&1")
    > ssh.exec!("setsid /root/test.pl > /dev/null 2>&1")
    > ssh.exec!("setsid /root/test.pl")
    >
    > Thanks.
    >
    > Enling


    OK, what about:

    ssh.exec!("sh -c 'setsid /root/test.pl > /dev/null 2>&1'")

    or perhaps bash, /bin/sh, or /bin/bash in place of sh

    Of course, I'm assuming that you are verifying that you can ssh
    manually and run these commands yourself. (If not, get to a command
    that works even after you log out manually.)

    -Rob

    > Rob Biedenharn wrote:
    >> On Jul 9, 2009, at 5:13 PM, Enling Li wrote:
    >>
    >>> The above code hangs also because test.pl contains a long sleep
    >>> and do
    >>>>
    >>>> I can do this from a shell by suffixing an '&':
    >>>>
    >>>> java -cp <classpath> <main_class>&
    >>>>
    >>>> starts the Java program in a background process thread and returns
    >>>> control the shell.
    >>>>
    >>>> When I try this from a Ruby program the program blocks until the
    >>>> Java
    >>>> program is terminated.

    >>
    >> If your system has the `setsid` (set session id) program (and the
    >> underlying system call), then you could try:
    >>
    >> ssh.exec!("setsid /root/test.pl")
    >>
    >> or
    >>
    >> setsid java -cp YOURCLASSPATH MainClass
    >>
    >> but you may wish to redirect output.
    >>
    >> ssh.exec!("setsid /root/test.pl >/tmp/test.log 2>&1")
    >>
    >> -Rob
    >>
    >> Rob Biedenharn http://agileconsultingllc.com
    >>

    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Rob Biedenharn, Jul 10, 2009
    #11
  12. Re: how can I start a shell process and return immediately?

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

    Are you sure you are properly connecting to the server? Have you tried just
    running something like ls and getting back a result? I just tried running a
    simple script that sleeps for 100 seconds and it ran just fine after the
    remote app was finished.

    If that still leaves you in trouble - you might want to look into Screen
    which is designed to run apps detached from the login shell so that would
    certainly be of use if needed. This is the command I use to run an app
    detached - screen -D -m command.rb

    John


    On Thu, Jul 9, 2009 at 6:34 PM, Enling Li <> wrote:

    > I tried that before. The thread won't solve the issue because once child
    > thread established it does the same way as main tread. When the main
    > thread terminates, the child gets killed also. It ends up nothing
    > happened to the shell command.
    >
    > Thanks.
    >
    > Enling
    >
    > John W Higgins wrote:
    > > On Thu, Jul 9, 2009 at 4:53 PM, Enling Li <>
    > > wrote:
    > >
    > >>

    > > Would just dropping the ssh section into it's own thread accomplish your
    > > task? That seems like a rather simple way of getting what you want.
    > >
    > > John

    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    John W Higgins, Jul 10, 2009
    #12
  13. Stephen Bannasch

    Enling Li Guest

    Re: how can I start a shell process and return immediately?

    John,

    The key is I don't want to wait for the remote to finish. If I wait for
    remote to complete, I don't have problem.

    Thanks.

    Enling


    John W Higgins wrote:
    > Are you sure you are properly connecting to the server? Have you tried
    > just
    > running something like ls and getting back a result? I just tried
    > running a
    > simple script that sleeps for 100 seconds and it ran just fine after the
    > remote app was finished.
    >
    > If that still leaves you in trouble - you might want to look into Screen
    > which is designed to run apps detached from the login shell so that
    > would
    > certainly be of use if needed. This is the command I use to run an app
    > detached - screen -D -m command.rb
    >
    > John


    --
    Posted via http://www.ruby-forum.com/.
    Enling Li, Jul 10, 2009
    #13
  14. Stephen Bannasch

    Enling Li Guest

    Re: how can I start a shell process and return immediately?

    Rob,

    That does not work either. I tried from shell befreo running the ruby
    script remotely.

    ssh.exec!("/bin/bash -c /usr/bin/setsid /usr/bin/perl /root/test.pl")
    ssh.exec!("/bin/bash /usr/bin/setsid /usr/bin/perl /root/test.pl")
    ssh.exec!("/bin/bash /usr/bin/setsid /root/test.pl")

    They just don't work from ruby Net::SSH

    Thanks.

    Enling
    Rob Biedenharn wrote:
    > On Jul 9, 2009, at 7:53 PM, Enling Li wrote:
    >
    >>
    >> Thanks.
    >>
    >> Enling

    >
    > OK, what about:
    >
    > ssh.exec!("sh -c 'setsid /root/test.pl > /dev/null 2>&1'")
    >
    > or perhaps bash, /bin/sh, or /bin/bash in place of sh
    >
    > Of course, I'm assuming that you are verifying that you can ssh
    > manually and run these commands yourself. (If not, get to a command
    > that works even after you log out manually.)
    >
    > -Rob


    --
    Posted via http://www.ruby-forum.com/.
    Enling Li, Jul 10, 2009
    #14
  15. Re: how can I start a shell process and return immediately?

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

    Enling,

    On Thu, Jul 9, 2009 at 8:06 PM, Enling Li <> wrote:

    > John,
    >
    > The key is I don't want to wait for the remote to finish. If I wait for
    > remote to complete, I don't have problem.
    >


    Sorry I used the wrong wording - I meant that Machine A connected to Machine
    B using Net::SSH and ran a script that slept for 100 seconds. Machine A then
    disconnected without blocking and Machine B continued to execute the script
    that was sleeping away. Again make sure you are connecting because you could
    easily be not getting to the server at all - the block could just as easily
    be a connection block and not an execution block. And if you are connecting
    and still for some reason blocking - Screen is definitely the answer because
    that is exactly what is it designed for.

    John



    > Thanks.
    >
    > Enling
    >
    >
    > John W Higgins wrote:
    > > Are you sure you are properly connecting to the server? Have you tried
    > > just
    > > running something like ls and getting back a result? I just tried
    > > running a
    > > simple script that sleeps for 100 seconds and it ran just fine after the
    > > remote app was finished.
    > >
    > > If that still leaves you in trouble - you might want to look into Screen
    > > which is designed to run apps detached from the login shell so that
    > > would
    > > certainly be of use if needed. This is the command I use to run an app
    > > detached - screen -D -m command.rb
    > >
    > > John

    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    John W Higgins, Jul 10, 2009
    #15
  16. Re: how can I start a shell process and return immediately?

    Enling Li wrote:
    > I have another quetion related to fire off a back ground shell process
    > on a remote Linux.
    >
    > require "net/ssh"
    >
    > ssh = Net::SSH.start( '10.0.77.87', 'toor', :password=>'logapp')
    > ssh.exec!("/root/test.pl &")
    > ssh.close
    > puts "I want to be here immediately, but not"
    >
    > The above code hangs also because test.pl contains a long sleep and do
    > nothing. Can someone offer me a help?
    >
    > Thank you in advance.
    >
    > Enling


    I run long running remote tasks that return immediately with...

    ssh.exec!("nohup /root/test.pl >/tmp/test.log 2>&1 &")
    --
    Posted via http://www.ruby-forum.com/.
    Andrew Kaspick, Jul 10, 2009
    #16
  17. Stephen Bannasch

    Enling Li Guest

    Re: how can I start a shell process and return immediately?

    Andrew,

    That is excellent. It resolved the issue.
    Thank you very much.

    Enling


    Andrew Kaspick wrote:
    > Enling Li wrote:
    >> I have another quetion related to fire off a back ground shell process
    >> on a remote Linux.
    >>
    >> require "net/ssh"
    >>
    >> ssh = Net::SSH.start( '10.0.77.87', 'toor', :password=>'logapp')
    >> ssh.exec!("/root/test.pl &")
    >> ssh.close
    >> puts "I want to be here immediately, but not"
    >>
    >> The above code hangs also because test.pl contains a long sleep and do
    >> nothing. Can someone offer me a help?
    >>
    >> Thank you in advance.
    >>
    >> Enling

    >
    > I run long running remote tasks that return immediately with...
    >
    > ssh.exec!("nohup /root/test.pl >/tmp/test.log 2>&1 &")


    --
    Posted via http://www.ruby-forum.com/.
    Enling Li, Jul 10, 2009
    #17
    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. Ram
    Replies:
    0
    Views:
    571
  2. klops
    Replies:
    0
    Views:
    352
    klops
    May 14, 2004
  3. Replies:
    6
    Views:
    8,216
  4. Samuel A. Falvo II
    Replies:
    0
    Views:
    468
    Samuel A. Falvo II
    Oct 8, 2008
  5. Victor 'Zverok' Shepelev

    Run the program and return immediately

    Victor 'Zverok' Shepelev, Feb 22, 2008, in forum: Ruby
    Replies:
    2
    Views:
    101
    Justin Collins
    Feb 22, 2008
Loading...

Share This Page