# Help with system call

**URL:** <https://www.ruby-forum.com/t/help-with-system-call/90003>\
**Category:** Rails\
**Created:** [March 24, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/help-with-system-call/90003 "2007-03-24T22:12:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 24, 2007, 10:12pm UTC](https://www.ruby-forum.com/t/help-with-system-call/90003/1 "2007-03-24T22:12:57Z")

</div>

Lemme start out by begging. I really need help with this one. Any  
suggestions at all. Now that that’s out of the way…

I’m trying to call script/generate from within Rails and having no luck.  
I’ve tried a couple of different ways.

# system “script/generate site\_contents X”

```
  which returns false when it fails but I can't get any information

```

other than the false return.

# `script/generate site_contents X` [in backticks]

```
  this just returns nil from within Rails even though from irb it

```

returns the STDOUT from the command.

I’m thanking you in advance because you’re really gonna save me on this  
one.  
Thank you thank you thank you. Please thank you.

RSL

---

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 24, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/help-with-system-call/90003/2 "2007-03-24T23:03:01Z")

</div>

Did you try adding “RAILS\_ROOT +” to the path to script/generate?

b

---

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 24, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/help-with-system-call/90003/3 "2007-03-24T23:12:02Z")

</div>

I actually did. 😕 I tried various File.expand\_path versions too. I even  
tried moving to the proper directory via Dir.chdir. 😕 Any other ideas?  
I’m  
just stumped and stymied.

RSL

---

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 25, 2007, 4:41am UTC](https://www.ruby-forum.com/t/help-with-system-call/90003/4 "2007-03-25T04:41:19Z")

</div>

Russell N. wrote:

> ```
> this just returns nil from within Rails even though from irb it
> 
> ```
> 
> returns the STDOUT from the command.
> 
> I’m thanking you in advance because you’re really gonna save me on this one.  
> Thank you thank you thank you. Please thank you.

This is the long way to do it just to show you the steps but this should  
work, at least under Unix/Linux:

cwd = `pwd`  
cwd.chomp! # pwd returns a LF at the end  
logger.debug(“cwd: #{cwd}”)  
generate = cwd + “/script/generate controller test \>  
#{cwd}/gen\_test.out”  
logger.debug(“generate: #{generate}”)  
system(generate)

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 25, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/help-with-system-call/90003/5 "2007-03-25T14:13:08Z")

</div>

I tried what you suggested, running those two rails\_generator requires  
and  
got a new error:

couldn’t find HOME environment – expanding `~’

which is, of course a common Windows error. Except that I’m running  
Unix.  
Lovely, right? Whoa! I just checked ENV.inspect and got an empty hash!  
I’m  
gonna have to look into this. Uh, this is not right, is it kittens?

RSL

---

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 25, 2007, 10:38am UTC](https://www.ruby-forum.com/t/help-with-system-call/90003/6 "2007-03-25T10:38:39Z")

</div>

Well, took a peek into the under the rails hood and here’s what’s in  
script/generate:

#!/usr/bin/env ruby  
require File.dirname( **FILE** ) + ‘/…/config/boot’  
require ‘commands/generate’

So, I found generate.rb in rails/lib/commands and here’s what it  
contains:

require “#{RAILS\_ROOT}/config/environment”  
require ‘rails\_generator’  
require ‘rails\_generator/scripts/generate’

ARGV.shift if [’–help’, ‘-h’].include?(ARGV[0])  
Rails::Generator::Scripts::Generate.new.run(ARGV)

Running this from within a rails app, you obviously already have the  
environment. Do the other two requires and you should be in business.

…

Yep… I just did the two requires in script/console, and then did:

Rails::Generator::Scripts::Generate.new.run([‘model’,‘foobar’])

And it generated the foobar.rb model, the test, the yaml, etc.

Neat.

b

---

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 25, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/help-with-system-call/90003/7 "2007-03-25T17:12:30Z")

</div>

Thankfully, I remembered the whole ENV[“RAILS\_ENV”] ||= “production”  
hack  
and have used this to set ENV[“HOME”]. Thank you so much to Michael and  
especially Ben for all your help. I’ve got a new problem now [with  
permissions on something in the generator process] but this one is  
solved  
and I’ll probably be able to hack around and find what’s up with this  
one.

Again, thanks! You really saved me and my sanity.

RSL

---

<div class="post-metadata">

**Author:** ![Russell\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Russell\_N](https://www.ruby-forum.com/u/Russell_N)\
**Post date:** [March 25, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/help-with-system-call/90003/8 "2007-03-25T16:19:19Z")

</div>

Okay. I’ve done some snooping and discovered that when I run ENV from  
the  
console I get something approximating all my shell variables but when I  
run  
it from an apache instance, I get nothing. I can set it [via SetEnv HOME  
/whatever] in the apache files but it never shows up in the ENV  
variable! I  
understand that the shell ENV and the Apache ENV are different but I’m  
not  
sure how to correctly set this now.

Help? I mean… Thank you for your future help! Please?

RSL
