Sunday, May 1, 2011

Ruby on Rails: How can I specify runner script environment

I am using a shell script to run some runner scripts in my Ruby on Rails app. I need to run it on the production database, but the following:

#!/bin/bash
/usr/bin/ruby RAILS_ENV=production ../script/runner ../lib/tasks.rb

gives an error:

/usr/bin/ruby: No such file or directory -- RAILS_ENV=production (LoadError)

I have tried to force it in config/environment.rb

ENV['RAILS_ENV'] ||= 'production'

or even

ENV['RAILS_ENV'] = 'production'

but even with that it still runs in development environment.

Update: I can force the scripts to connect to the right database by editing the config/database.yml file, but I wonder what's the proper way of doing it.

From stackoverflow
  • If that's your command, the order of your arguments is your biggest problem.

    /usr/bin/ruby RAILS_ENV=production ../script/runner ../lib/tasks.rb
    

    Is different than.

    /usr/bin/ruby ../script/runner ../lib/tasks.rb RAILS_ENV=production
    

    The second example is looking for the file, the first one is setting a runtime variable while ruby interpreting it as the file you want to run.

  • If you redo your script like this:

    #!/bin/bash
    RAILS_ENV=production
    /usr/bin/ruby ../script/runner ../lib/tasks.rb
    

    ...that will make it stick for the lifetime of the script. To make it stick for the lifetime of the shell's session, change it to

    #!/bin/bash
    export RAILS_ENV=production
    /usr/bin/ruby ../script/runner ../lib/tasks.rb
    
  • You can set the environment variable like this :

    RAILS_ENV=production /usr/bin/ruby ../script/runner ../lib/tasks.rb
    
  • The help on the command line for script/runner gives you your answer.

    script/runner -e production Model.method
    
    Chirantan : Faced the same issue. This solution worked for me. Thanks. +1

0 comments:

Post a Comment