# Exec (new process or new thread?) to continue

**URL:** <https://www.ruby-forum.com/t/exec-new-process-or-new-thread-to-continue/161230>\
**Category:** Ruby\
**Created:** [February 24, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/exec-new-process-or-new-thread-to-continue/161230 "2009-02-24T14:20:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [February 24, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/exec-new-process-or-new-thread-to-continue/161230/1 "2009-02-24T14:20:47Z")

</div>

Hi,

I have fired off an exe with the kernal method exec

loader\_app = “#{Dir.getwd}/PublishUi.exe”  
exec(loader\_app)

The exec method seems to hold up the process

so

exec(loader\_app)  
puts “we got here”

Will not print “we got here” till I close the app.

I am not sure whether I should use a new thread or a new process here,  
or whether there is an alternative.

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [February 24, 2009, 2:30pm UTC](https://www.ruby-forum.com/t/exec-new-process-or-new-thread-to-continue/161230/2 "2009-02-24T14:30:47Z")

</div>

On Feb 24, 2009, at 8:19 AM, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Aidy

IO.popen(an\_exe)

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [February 24, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/exec-new-process-or-new-thread-to-continue/161230/3 "2009-02-24T14:59:43Z")

</div>

On Tue, Feb 24, 2009 at 6:59 PM, List.rb [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> IO.popen(an\_exe)

And with ruby 1.9, Kernel.spawn:  
[http://ruby-doc.org/core-1.9/classes/Kernel.html#M006071](http://ruby-doc.org/core-1.9/classes/Kernel.html#M006071)

Cheers,  
lasitha.

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [February 24, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/exec-new-process-or-new-thread-to-continue/161230/4 "2009-02-24T21:07:17Z")

</div>

aidy wrote:

> Hi,
> 
> I have fired off an exe with the kernal method exec
> 
> loader\_app = “#{Dir.getwd}/PublishUi.exe”  
> exec(loader\_app)

Since you appear to be on windows, you can’t fork, but you can do this:

Thread.new do  
loader\_app = “#{Dir.getwd}/PublishUi.exe”  
system(loader\_app)  
end

# continue with other stuff here

This is a fairly cross-platform way to handle it. Of course on windows,  
you can also do

system “start …”

and then you don’t even need a ruby thread.

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [February 24, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/exec-new-process-or-new-thread-to-continue/161230/5 "2009-02-24T15:00:53Z")

</div>

aidy wrote:

> Aid  
> exec doesn’t hold up the process, it replaces it completely[1]. Your  
> second example will never print “we got here”.

Assuming you do not need to communicate with, wait on, or know anything  
about the process you are starting, you can use fork[2] and exec this  
way:

fork { exec load\_app }

-Justin

[1] [module Kernel - RDoc Documentation](http://ruby-doc.org/core/classes/Kernel.html#M005979)  
[2] [module Kernel - RDoc Documentation](http://ruby-doc.org/core/classes/Kernel.html#M005980)

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [April 8, 2009, 10:35pm UTC](https://www.ruby-forum.com/t/exec-new-process-or-new-thread-to-continue/161230/6 "2009-04-08T22:35:22Z")

</div>

I like this pattern:

def fire\_and\_forget(&block)  
pid = fork do  
begin  
yield  
ensure  
Process.exit!  
end  
end  
Process.detach pid  
end

A little bit safer then just using ‘fork’…

Taken from a post here:  
[http://www.caboo.se/articles/2006/10/14/premcache-caching-an](http://www.caboo.se/articles/2006/10/14/premcache-caching-an)  
d-precaching-with-memcached

Justin C. wrote:

> Assuming you do not need to communicate with, wait on, or know anything  
> about the process you are starting, you can use fork[2] and exec this  
> way:
> 
> fork { exec load\_app }
> 
> -Justin
> 
> [1] [module Kernel - RDoc Documentation](http://ruby-doc.org/core/classes/Kernel.html#M005979)  
> [2] [module Kernel - RDoc Documentation](http://ruby-doc.org/core/classes/Kernel.html#M005980)
