# Starting a process from inside Ruby

**URL:** <https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710>\
**Category:** Ruby\
**Created:** [February 19, 2009, 2:08am UTC](https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710 "2009-02-19T02:08:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [February 19, 2009, 2:08am UTC](https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710/1 "2009-02-19T02:08:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [February 19, 2009, 2:18am UTC](https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710/2 "2009-02-19T02:18:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [February 19, 2009, 4:20am UTC](https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710/3 "2009-02-19T04:20:47Z")

</div>

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…

> **[Multiple Threads and Processes in Ruby](https://andrejserafim.wordpress.com/2007/12/16/multiple-threads-and-processes-in-ruby/)**
>
> Threads At the moment (ruby version 1.8 ) one can either branch off a different thread, which will be handled by the same ruby interpreter as the parent. Which means that all variables created outs…

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 M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, February 18, 2009 5:18 PM  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Starting a process from inside Ruby

---

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [February 19, 2009, 7:00am UTC](https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710/4 "2009-02-19T07:00:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [February 19, 2009, 2:02pm UTC](https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710/5 "2009-02-19T14:02:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [February 19, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710/6 "2009-02-19T14:24:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![petrjanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee7513/32.png) [@petrjanda](https://www.ruby-forum.com/u/petrjanda)\
**Post date:** [February 19, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/starting-a-process-from-inside-ruby/160710/7 "2009-02-19T18:27:37Z")

</div>

On Thu, Feb 19, 2009 at 8:23 AM, Petr J.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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/](http://backgroundrb.rubyforge.org/)

–  
Rick DeNatale

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