# Executing an external application within rails

**URL:** <https://www.ruby-forum.com/t/executing-an-external-application-within-rails/50201>\
**Category:** Rails\
**Created:** [December 14, 2005, 2:46am UTC](https://www.ruby-forum.com/t/executing-an-external-application-within-rails/50201 "2005-12-14T02:46:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Conway](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@Jonathan\_Conway](https://www.ruby-forum.com/u/Jonathan_Conway)\
**Post date:** [December 14, 2005, 2:46am UTC](https://www.ruby-forum.com/t/executing-an-external-application-within-rails/50201/1 "2005-12-14T02:46:53Z")

</div>

Hi,  
I seem to have come across a strange problem. I need to execute an  
external application, grab its return value and do something with it.  
Sounds fine doesn’t it? Unfortunetely I’m running into some problems.

Just for example sake, lets say I want to execute the date command on  
unix I’d do something like test = %x{date} or test IO.popen(‘date’).read  
and I should get a value back assigned to test right? Well it seems it  
only works from inside IRB and using the rails console.

When I execute this code from within a method in a Rails application I  
get nothing back. Writing the output to the log show nothing either.

Is there something I’m missing?

Cheers

Jon

---

<div class="post-metadata">

**Author:** ![Jonathan\_Conway](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@Jonathan\_Conway](https://www.ruby-forum.com/u/Jonathan_Conway)\
**Post date:** [December 14, 2005, 4:55am UTC](https://www.ruby-forum.com/t/executing-an-external-application-within-rails/50201/2 "2005-12-14T04:55:31Z")

</div>

Your PATH environment variable or a fully qualified path to date?

– -- Tom M.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Conway](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@Jonathan\_Conway](https://www.ruby-forum.com/u/Jonathan_Conway)\
**Post date:** [December 14, 2005, 1:05pm UTC](https://www.ruby-forum.com/t/executing-an-external-application-within-rails/50201/3 "2005-12-14T13:05:36Z")

</div>

Thank I’ve got a little bit further. It now works for date using the  
full path /bin/date but if say do something like %x{/usr/local/bin/svn}  
(this is just a test example), I thought I should at least get the svn  
message asking me for the parameters, but instead I get nothing back.  
Very strange.

It works without problem from /script/console and irb… just not withing  
a rails app.

Any help greatly appreciated

Jon

---

<div class="post-metadata">

**Author:** ![Jonathan\_Conway](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@Jonathan\_Conway](https://www.ruby-forum.com/u/Jonathan_Conway)\
**Post date:** [December 14, 2005, 3:48pm UTC](https://www.ruby-forum.com/t/executing-an-external-application-within-rails/50201/4 "2005-12-14T15:48:15Z")

</div>

Jonathan Conway wrote:

> Thank I’ve got a little bit further. It now works for date using the  
> full path /bin/date but if say do something like %x{/usr/local/bin/svn}  
> (this is just a test example), I thought I should at least get the svn  
> message asking me for the parameters, but instead I get nothing back.  
> Very strange.

If you run “svn” with no arguments it will return a message to you by  
writing to STDERR. When you  
see the returned text in irb or script/console you are not getting a  
return value, you are simply  
seeing the message printed to STDERR.

%x{ } and ` ` both return strings received from STDOUT only

If you want to capture all output you can use Open3.popen which is  
available on \*nix systems, and  
you can also get it on Windows system by downloading the package from  
win32-open3 package from  
[http://rubyforge.org/projects/win32utils/](http://rubyforge.org/projects/win32utils/)

require ‘open3’  
stdin,stdout,stderr = Open3.popen “svn”  
stderr.read # =\> “Type ‘svn help’ for usage.\n”

Zach
