# Run ruby script.rb from rails

**URL:** <https://www.ruby-forum.com/t/run-ruby-script-rb-from-rails/109150>\
**Category:** Rails\
**Created:** [July 11, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/run-ruby-script-rb-from-rails/109150 "2007-07-11T22:43:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkitchel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@dkitchel](https://www.ruby-forum.com/u/dkitchel)\
**Post date:** [July 11, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/run-ruby-script-rb-from-rails/109150/1 "2007-07-11T22:43:01Z")

</div>

I’ve spent way too long searching for a way to run a ruby script which  
parses and ingests an xml document as named by a parameter into mysql.

on the console it works fine - script/myscript myxmlfile

I just need to be able to execute this same ruby script with a parameter  
or two.

I need to be able to do this each time someone uploads a file so it  
populates a database with their userid etc, so, I don’t want to use a  
cron.

Someone please help me. I can’t find anything on this topic.

---

<div class="post-metadata">

**Author:** ![dkitchel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@dkitchel](https://www.ruby-forum.com/u/dkitchel)\
**Post date:** [July 11, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/run-ruby-script-rb-from-rails/109150/2 "2007-07-11T22:51:21Z")

</div>

try this:

result = `ruby /full/path/to/script.rb`

(note the ` instead of ')

Davis K. wrote:

> I’ve spent way too long searching for a way to run a ruby script which  
> parses and ingests an xml document as named by a parameter into mysql.
> 
> on the console it works fine - script/myscript myxmlfile
> 
> I just need to be able to execute this same ruby script with a parameter  
> or two.
> 
> I need to be able to do this each time someone uploads a file so it  
> populates a database with their userid etc, so, I don’t want to use a  
> cron.
> 
> Someone please help me. I can’t find anything on this topic.

---

<div class="post-metadata">

**Author:** ![dkitchel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@dkitchel](https://www.ruby-forum.com/u/dkitchel)\
**Post date:** [July 11, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/run-ruby-script-rb-from-rails/109150/3 "2007-07-11T23:07:24Z")

</div>

It looks promising, but I can’t get that to work

\<% result = `ruby /private/var/www/myapp/script/myscript.rb` %\>

Where do you suggest I put that code? And I assume you mean that I do  
nothing with the result? it just prompts the code to run?

Dustin Frisell wrote:

> try this:
> 
> result = `ruby /full/path/to/script.rb`
> 
> (note the ` instead of ')
> 
> Davis K. wrote:
> 
> > I’ve spent way too long searching for a way to run a ruby script which  
> > parses and ingests an xml document as named by a parameter into mysql.
> > 
> > on the console it works fine - script/myscript myxmlfile
> > 
> > I just need to be able to execute this same ruby script with a parameter  
> > or two.
> > 
> > I need to be able to do this each time someone uploads a file so it  
> > populates a database with their userid etc, so, I don’t want to use a  
> > cron.
> > 
> > Someone please help me. I can’t find anything on this topic.

---

<div class="post-metadata">

**Author:** ![dkitchel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@dkitchel](https://www.ruby-forum.com/u/dkitchel)\
**Post date:** [July 12, 2007, 10:58am UTC](https://www.ruby-forum.com/t/run-ruby-script-rb-from-rails/109150/4 "2007-07-12T10:58:42Z")

</div>

Davis K. wrote:

> It looks promising, but I can’t get that to work
> 
> \<% result = `ruby /private/var/www/myapp/script/myscript.rb` %\>
> 
> Where do you suggest I put that code? And I assume you mean that I do  
> nothing with the result? it just prompts the code to run?

Everything that is sent to command line will be stored in the variable  
‘result’ and you can do whatever you want with it.

By the way if you want it to show, it should be

\<%= `ruby /private/var/www/myapp/script/myscript.rb` %\>

The way you have it right now only runs the code, doesn’t display the  
output from doing that.

Cheers,  
Mohit.  
7/12/2007 | 4:54 PM.

---

<div class="post-metadata">

**Author:** ![dkitchel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@dkitchel](https://www.ruby-forum.com/u/dkitchel)\
**Post date:** [July 12, 2007, 11:16am UTC](https://www.ruby-forum.com/t/run-ruby-script-rb-from-rails/109150/5 "2007-07-12T11:16:29Z")

</div>

Hi,

I’m having the same kind of problem but on my side I would like to run  
it from my controller and not from the view. Can I do that in the same  
way ??

And I have to pass parameters to this script, but they are store in a  
variable. Do you know how I can do that ??

Thank’s

Franz

Mohit S. wrote:

> Davis K. wrote:
> 
> > It looks promising, but I can’t get that to work
> > 
> > \<% result = `ruby /private/var/www/myapp/script/myscript.rb` %\>
> > 
> > Where do you suggest I put that code? And I assume you mean that I do  
> > nothing with the result? it just prompts the code to run?
> 
> Everything that is sent to command line will be stored in the variable  
> ‘result’ and you can do whatever you want with it.
> 
> By the way if you want it to show, it should be
> 
> \<%= `ruby /private/var/www/myapp/script/myscript.rb` %\>
> 
> The way you have it right now only runs the code, doesn’t display the  
> output from doing that.
> 
> Cheers,  
> Mohit.  
> 7/12/2007 | 4:54 PM.
