Starting a process from inside Ruby

P

Petr Janda

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
 
M

Michael Malone

Petr said:
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.
=======================================================================
 
J

Jayce Meade

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

http://andrejserafim.wordpress.com/2007/12/16/multiple-threads-and-processes-in-ruby/

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

- jayce
 
D

David Masover

Petr said:
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.
 
P

Petr Janda

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

Petr Janda

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
 
R

Rick DeNatale

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

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!

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/

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

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,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top