Forum: Ruby Starting a process from inside Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
40920c2420f2ed4841ff4ed2fc34c99f?d=identicon&s=25 Petr Janda (petrjanda)
on 2009-02-19 02:08
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
770048af205ed307b8cf35ae2282ee2f?d=identicon&s=25 Michael Malone (Guest)
on 2009-02-19 02:18
(Received via mailing list)
Petr Janda wrote:
> 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.
=======================================================================
3d61a8dbbd64d0feed8698689cf15276?d=identicon&s=25 Jayce Meade (Guest)
on 2009-02-19 04:20
(Received via mailing list)
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/mult...

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
1bc63d01bd3fcccc36fb030a62039352?d=identicon&s=25 David Masover (Guest)
on 2009-02-19 07:00
(Received via mailing list)
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.
40920c2420f2ed4841ff4ed2fc34c99f?d=identicon&s=25 Petr Janda (petrjanda)
on 2009-02-19 14:02
> 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.
40920c2420f2ed4841ff4ed2fc34c99f?d=identicon&s=25 Petr Janda (petrjanda)
on 2009-02-19 14:24
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
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-02-19 18:27
(Received via mailing list)
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!
>

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
This topic is locked and can not be replied to.