# Ruby subprocess

**URL:** <https://www.ruby-forum.com/t/ruby-subprocess/86731>\
**Category:** Ruby\
**Created:** [February 25, 2007, 5:56am UTC](https://www.ruby-forum.com/t/ruby-subprocess/86731 "2007-02-25T05:56:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![badcherry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@badcherry](https://www.ruby-forum.com/u/badcherry)\
**Post date:** [February 25, 2007, 5:56am UTC](https://www.ruby-forum.com/t/ruby-subprocess/86731/1 "2007-02-25T05:56:01Z")

</div>

I am wanting inside of a ruby script to execute another ruby script in  
a subprocess in the same manner as exec(). As first thought I  
considered seeing if you can lookup the ruby executable path from  
within ruby, but I couldn’t find a way of doing it. If that was the  
case you could just say exec("/usr/bin/ruby " + “file.rb”).

---

<div class="post-metadata">

**Author:** ![badcherry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@badcherry](https://www.ruby-forum.com/u/badcherry)\
**Post date:** [February 25, 2007, 6:00am UTC](https://www.ruby-forum.com/t/ruby-subprocess/86731/2 "2007-02-25T06:00:01Z")

</div>

On Sun, 25 Feb 2007, badcherry wrote:

> I am wanting inside of a ruby script to execute another ruby script in  
> a subprocess in the same manner as exec(). As first thought I  
> considered seeing if you can lookup the ruby executable path from  
> within ruby, but I couldn’t find a way of doing it. If that was the  
> case you could just say exec("/usr/bin/ruby " + “file.rb”).

```
 require 'rbconfig'

 def which_ruby
   c = ::Config::CONFIG
   File::join(c['bindir'], c['ruby_install_name']) << c['EXEEXT']
 end

 ruby = which_ruby

 exec ruby, 'file.rb'

```

probably won’t work on windows since the one-click breaks (or used to  
break)  
rbconfig.

-a

---

<div class="post-metadata">

**Author:** ![badcherry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@badcherry](https://www.ruby-forum.com/u/badcherry)\
**Post date:** [February 25, 2007, 1:06pm UTC](https://www.ruby-forum.com/t/ruby-subprocess/86731/3 "2007-02-25T13:06:30Z")

</div>

On 25.02.2007 05:52, badcherry wrote:

> I am wanting inside of a ruby script to execute another ruby script in  
> a subprocess in the same manner as exec(). As first thought I  
> considered seeing if you can lookup the ruby executable path from  
> within ruby, but I couldn’t find a way of doing it. If that was the  
> case you could just say exec("/usr/bin/ruby " + “file.rb”).

Depending on the platform you can use fork like this:

child\_pid = fork do

# child code

load “file.rb”  
end

# …

Process.wait(child\_pid)  
print "Child ", child\_pid, " exited with ", $?.exitstatus, “\n”

See  
[http://www.ruby-doc.org/core/classes/Kernel.html#M005750](http://www.ruby-doc.org/core/classes/Kernel.html#M005750)  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut\_threads.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_threads.html)

HTH

robert

---

<div class="post-metadata">

**Author:** ![badcherry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@badcherry](https://www.ruby-forum.com/u/badcherry)\
**Post date:** [February 25, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/ruby-subprocess/86731/4 "2007-02-25T14:50:52Z")

</div>

On Feb 24, 10:59 pm, removed\_email\_address@domain.invalid wrote:

> ```
> exec ruby, 'file.rb'
> 
> ```
> 
> probably won’t work on windows since the one-click breaks (or used to break)  
> rbconfig.

This will work for now, but didn’t test it on windows yet. Thanks,  
much.
