Starting a process from inside Ruby

Hi all,
Im facing a small dilema trying to start a process called "ezstream"
from inside a ruby on rails application and retreive its PID.

Ezstream is used for streaming audio into an icecast server so normally
this process runs in background for a long time. I tried it with
IO.popen but then the application hangs indefinitely. What I need to do
is start the app, get its PID(to store it in a databasse), and move it
to the background so the rails application doesnt hang.

Any ideas?
Petr

···

--
Posted via http://www.ruby-forum.com/.

Petr Janda wrote:

Hi all,
Im facing a small dilema trying to start a process called "ezstream"
from inside a ruby on rails application and retreive its PID.

Ezstream is used for streaming audio into an icecast server so normally
this process runs in background for a long time. I tried it with
IO.popen but then the application hangs indefinitely. What I need to do
is start the app, get its PID(to store it in a databasse), and move it
to the background so the rails application doesnt hang.

Any ideas?
Petr
  

I don't know if this works inside rails but the Kernel#fork method is a good idea!

pid = fork() do
    #Ezstream stuff
end
#store pid in db
...
wait(pid)

···

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.

Petr Janda wrote:

What I need to do
is start the app, get its PID(to store it in a databasse), and move it
to the background so the rails application doesnt hang.
  
I think the other posts are right... If it's not part of your app, by the way, you could do:

pid = fork { exec('ezstream') }

Assuming 'ezstream' is the command you'd have to run.

Any ideas?

Well, since you asked...

This problem has been solved over and over again in Rails. Look up workling/starling, nanite (or whatever it's called now), backgroundrb, drb, monit, god, etc, ad nauseum.

I'm not saying that there's no possibility of doing it better -- I know I had a similar problem recently that I solved with an old-fashioned daemon and polling. Just probably worth looking at what's already out there before you reinvent the wheel.

Ok ive had some success with using threads instead of forks. Im able to
retreive the pid of the ezstream easily process because Threads share
variables!

Thanks.
Petr

···

--
Posted via http://www.ruby-forum.com/.

Fork would work from what I've read, I believe it starts the other process and continues execution of the current ruby script, the external process runs until it is (from what I understand) terminated by the parent script, normal exit, and the like.

This should help, it covers the basic process management... features? provided by ruby, including threads, system(), exec(), fork(), etc..

On this subject, when I use fork, I get an error saying it hasn't been implemented, how would I fix this?

- jayce

···

--------------------------------------------------
From: "Michael Malone" <michael.malone@tait.co.nz>
Sent: Wednesday, February 18, 2009 5:18 PM
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Subject: Re: Starting a process from inside Ruby

Petr Janda wrote:

Hi all,
Im facing a small dilema trying to start a process called "ezstream"
from inside a ruby on rails application and retreive its PID.

Ezstream is used for streaming audio into an icecast server so normally
this process runs in background for a long time. I tried it with
IO.popen but then the application hangs indefinitely. What I need to do
is start the app, get its PID(to store it in a databasse), and move it
to the background so the rails application doesnt hang.

Any ideas?
Petr

I don't know if this works inside rails but the Kernel#fork method is a good idea!

pid = fork() do
   #Ezstream stuff
end
#store pid in db
...
wait(pid)

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.

pid = fork { exec('ezstream') }

Assuming 'ezstream' is the command you'd have to run.

Hmm there seems to be an issue running this code in rails as the 2nd
process never seems to start although ezstream does get started. It also
produces Application Error message.

···

--
Posted via http://www.ruby-forum.com/\.

You might also want to have a look at backgroundrb which is the normal way
to do background jobs in Rails.

http://backgroundrb.rubyforge.org/

···

On Thu, Feb 19, 2009 at 8:23 AM, Petr Janda <elekktretterr@exemail.com.au>wrote:

Ok ive had some success with using threads instead of forks. Im able to
retreive the pid of the ezstream easily process because Threads share
variables!

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale