# Exec() not returning to ruby

**URL:** <https://www.ruby-forum.com/t/exec-not-returning-to-ruby/63880>\
**Category:** Ruby\
**Created:** [June 20, 2006, 6:29am UTC](https://www.ruby-forum.com/t/exec-not-returning-to-ruby/63880 "2006-06-20T06:29:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![craig\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@craig\_b](https://www.ruby-forum.com/u/craig_b)\
**Post date:** [June 20, 2006, 6:29am UTC](https://www.ruby-forum.com/t/exec-not-returning-to-ruby/63880/1 "2006-06-20T06:29:00Z")

</div>

I’ve got some code like this:

sources = File.new ‘file\_list.txt’  
sources.each do |src|  
exec “mkdir #{some\_dest}” unless File.exists some\_dest  
exec “svn export #{src} #{some\_dest}”  
end  
puts ‘Done.’

The problem is that ruby calls ‘svn export’ once for the first line  
of sources and exits without looping through subsequent sources or  
getttng to the line “puts ‘Done.’”

exec works just fine with the “mkdir” call.

A couple of things:

1. I’m on Windows Server 2003
2. svn (Subversion) 1.3
3. I’m not using the rscm gem with it’s subversion interface because  
I need to export and rscm doesn’t appear to do this (maybe I’ll see  
about adding it)

Any ideas why a call to svn wouldn’t return control to my ruby script?

–  
Craig B.

[http://www.luckybonza.com](http://www.luckybonza.com)  
AIM: kreiggers

---

<div class="post-metadata">

**Author:** ![craig\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@craig\_b](https://www.ruby-forum.com/u/craig_b)\
**Post date:** [June 20, 2006, 7:29am UTC](https://www.ruby-forum.com/t/exec-not-returning-to-ruby/63880/2 "2006-06-20T07:29:51Z")

</div>

On Jun 20, 2006, at 12:27 AM, Craig B. wrote:

> of sources and exits without looping through subsequent sources or  
> getttng to the line “puts ‘Done.’”
> 
> exec works just fine with the “mkdir” call.
> 
> A couple of things:
> 
> 1. I’m on Windows Server 2003

I’m not a windows guy so I don’t know exactly how exec is interpreted  
on Windows, but on the Unix side:

exec causes the currently running program (i.e. Ruby) to be replaced  
with the new command.  
Your “svn” command doesn’t run because the first exec effectively  
replaced the Ruby interpreter with  
“mkdir”. Sort of like invasion of the body-snatchers. First the  
process is running Ruby and then it  
is running “mkdir” and Ruby is gone.

The Unix idiom to handle what you want to do is to call fork first to  
create a child process and then  
have the child process be replaced with the command via ‘exec’.  
Rinse and repeat.

In your case, you might be better off using ‘system’ instead of  
‘exec’. System will take care of  
forking and exec’ing the command for you.

Gary W.

---

<div class="post-metadata">

**Author:** ![craig\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@craig\_b](https://www.ruby-forum.com/u/craig_b)\
**Post date:** [June 20, 2006, 8:42am UTC](https://www.ruby-forum.com/t/exec-not-returning-to-ruby/63880/3 "2006-06-20T08:42:58Z")

</div>

you should be use ‘system’ instead of ‘exec’.  
look like this,  
system ‘dir’  
or  
`dir`

both ok.

---

<div class="post-metadata">

**Author:** ![craig\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@craig\_b](https://www.ruby-forum.com/u/craig_b)\
**Post date:** [June 20, 2006, 8:45am UTC](https://www.ruby-forum.com/t/exec-not-returning-to-ruby/63880/4 "2006-06-20T08:45:36Z")

</div>

because ‘exec’ will replace the current process by running the given  
external command.so

exec ‘dir’

# never get here

u know?🙂  
you can type ‘ri exec’ @ shell for more…

enjoy

---

<div class="post-metadata">

**Author:** ![craig\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/76d3ee/32.png) [@craig\_b](https://www.ruby-forum.com/u/craig_b)\
**Post date:** [June 22, 2006, 5:13am UTC](https://www.ruby-forum.com/t/exec-not-returning-to-ruby/63880/5 "2006-06-22T05:13:54Z")

</div>

Great!

system did the trick

between php, ruby, linux and mac osx at home, c# and windows at work  
I have a hard time switching modes and remembering which is which.

On Jun 19, 2006, at 10:26 PM, removed\_email\_address@domain.invalid wrote:

> > puts ‘Done.’  
> > I’m not a windows guy so I don’t know exactly how exec is  
> > The Unix idiom to handle what you want to do is to call fork first

–  
Craig B.

[http://www.luckybonza.com](http://www.luckybonza.com)  
AIM: kreiggers
