Skip navigation

This post has moved here:



  1. newbie question…
    what is your directory structure? is ‘account’ under ‘app/controllers/’


  2. Yeah, basically I am executing a method of a controller, so I have app/controllers/account_controller.rb (my controller), with a “send_newsletters” action.

  3. You should actually consider adding this tip of your to the rails wiki page for RunnerScript

  4. No problem, Uri – all done.

  5. I’m needing to do the same thing as you, except I think I will jut be using the runner script to run my mailer. Anyways, I was wondering how you were able to use ActiveMailer for mass mailing. I am going to try using sendmail as the smtp method creates a new connection with the server for every message. Have you been using sendmail? Have you ran into any problems the way you are doing it?

  6. At the moment, we’ve just used ActiveMailer – it’s not a permanent solution, just a solution that allows us to get the more important stuff done for now.

  7. Hi,

    I am trying to use your script with rails 1.2 but it failed.

    ./script/runner -e development “app =; app.get ‘packages/update_repository'”

    I got the following error:
    /lib/active_support/dependencies.rb:263:in `load_missing_constant’: uninitialized constant ActionController::Integration (NameError)

    Did I missed something ?

  8. Re. “With any luck, this will help another person save a couple of minutes!” – more than a couple of minutes – you probably just saved me a couple of hours. Thanks, this works beautifully.

  9. Actually, I spoke a little too soon, as although I was able to run the script properly from the command line, when I made it a CRON job it didn’t work any more. Judging by the errors I was getting, it appeared that it lacked the necessary environment when it was running as a CRON.

    It turned out that the .bash_profile file was not being processed on our web host when the CRON job ran, so I had to put in the environment initialization myself. This is what ended up working as the script:

    export PATH=$PATH:/home/[SITE#]/data/rubygems/bin:/home/[SITE#]/data/rubygems/gems/bin
    export RUBYLIB=/home/[SITE#]/data/rubygems/local/lib/site_ruby/1.8
    export GEM_HOME=/home/[SITE#]/data/rubygems/gems

    cd /home/[SITE#]/containers/rails/[APPNAME]
    echo “generating XML files for flash front-end…”
    ./script/runner -e production “app =; app.get ‘xml/build_xml'”

    Where [SITE#] identified our site in the particular structure of this web host, and [APPNAME] was the Rails app directory. Since this is likely different on your host, you should be able to customize this according to the location of rubygems on your host.

    Good luck!

  10. Interestingly, I get the same error as Duffman.

    $ ruby script/runner -e development “app =; app.get ‘links/hot'”
    /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/runner.rb:47: /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:263:in `load_missing_constant’: uninitialized constant ActionController::Integration (NameError)
    from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:452:in `const_missing’
    from (eval):1
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `eval’
    from /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/runner.rb:47
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
    from script/runner:3

    I’m running Rails 1.2.2

  11. I too have the same error as Railsroad and Duffman. I’m also using Rails 1.2.2

    Anyone any ideas what the probem can be?

  12. Did anyone get it to work finally? I have the same load_missing_constant error

  13. you have to require “action_controller/integration” explicit.

    I realized the same behaviour within a rake-task:

    in “lib/tasks”
    (some rb-file)
    require “action_controller/integration”
    desc “Erzeuge aktuelle Monatsreports ”
    task :monatsreport => :environment do
    app =;
    Portfolio.find( :all ).each do | p |
    app.get “monatsreport/display_pdf/#{}?long=true”

    which is started with
    # rake monatsreport RAILS_ENV=production
    from RAILS_ROOT

    Summergreetings from Stuttgart

  14. Using rake might be the nicer solution. However, the file has to be named ‘XXX.rake’ and I had to include a “require(‘action_controller’)” before the “require(‘action_controller/integration’)” to make it work.

  15. To Sadat, Duffman, Koen:

    require ‘action_controller/integration’

    to the end of your environment.rb file

  16. Hi!
    Currently we’re having a problem in a project with this kind of rake task. We want to use ActiveRecord based session management, but if we uncomment the specific line in environment.rb the tasks don’t work any more. Even rake db:migrate doesn’t work. It always says: “uninitialized constant CGI:Session…”

    The problem seems to be the require (‘action_controller’) line, because when I comment it out the rake tasks work again, even rake db:migrate.

    Did anybody else have this problem when using ActiveRecord based session management? Or a workaround?

    Thanks a lot!

  17. Inspite of adding the

    I get the following error:

    Route segment “:controller” cannot be optional because it precedes a required se
    gment. This segment will be required.

    Does anyone know what I am missing?


  18. I meant,
    Inspite of adding the

    require ‘action_controller/integration’

    to the end of my environment.rb file

  19. @Hari: That sounds like a problem in your routes rather than a missing require.

  20. Hi,

    I write a little article on how to make all this work inside a rake task:

    Hope it helps someone!


  21. I tried in vain to get this to work, but had much better luck running my task as a rake command using this tutorial:

  22. Just a warning 🙂

    If you use this solution in some more complex code, be warned that Rails may automatically unload jour classes after app.get() finishes.

    For example (just idea, not working code):
    class FooRunner
    def process
    @app.get “/index” ‘Done’ # this line..
    ..if run from script/runner, may raise:
    ..`load_missing_constant’: A copy of FooRunner has been removed from the module tree but is still active! (ArgumentError)

    “When the request is completed, each autoloaded constant is removed, leaving the process in a clean state.”

    Or, you may be hit by strange errors:
    /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/runner.rb:47: undefined method `children’ for # (NoMethodError)

    ..besides that usually Position happily responds to ‘children’.

  23. Ive tried it without luck. The output is:

    Loaded suite ./script/runner

    Finished in 0.000191 seconds.

    0 tests, 0 assertions, 0 failures, 0 errors

    And none happens

  24. Me too, but I’m faster “Finished in 0.0 seconds” .

    D:\SmartUver>ruby script/runner –verbose=verbose “app =; app.get ‘zadosti/predschvaleni_novych_zadosti'”

    Loaded suite script/runner

    Finished in 0.0 seconds.

    0 tests, 0 assertions, 0 failures, 0 errors

    And nothing happens (Rails 1.2.3 on WinXP)

  25. Thanks for this tip. It helps me a lot.

  26. Hello!

    I was having problems with the Rails gem not being found. Turns out, although Ade’s comment got me almost there, I still had to set my GEM_PATH.

    GEM_HOME is where RubyGems writes to, but GEM_PATH is where gem (and therefore Rails, when rubygems in included) reads from.

    My final ended up looking like this:

    export PATH=$PATH:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/deploy/bin:/usr/sbin:/sbin
    export RUBYLIB=$RUBYLIB:/usr/lib/site_ruby/1.8

    export GEM_HOME=$GEM_HOME:/usr/lib/ruby/gems/1.8/gems
    export GEM_PATH=$GEM_PATH:/usr/lib/ruby/gems/1.8

    # This is how I figured out it was broken:
    #gem query

    cd /var/www/apps/bunnies/current

    echo “Doing work…”
    # Output is not important, as it’s non-informative. Check production.log in the app
    ./script/runner -e production ‘Model.some_method’ > /dev/null
    echo “Done.”

  27. hi all, I want to call the function from application.rb when executing runner but it doesn work with: ApplicationController.function(). Please help me on this.

  28. Save me quite a bit more than “couple of minutes” 🙂 Thanks!

  29. For what its worth, you can put the “require …” line in the script runner line.

    E.g., ruby script/runner “require ‘action_controller/integration’; app =; app.get(‘account/send_newsletters’);”

  30. Well… This only works for the default environment. If you try to change the environment with a -e argument, the require seems to load a different runner and fails when it tries to parse the -e option. Solution: set the default environment as you are good to go…

    /usr/bin/env RAILS_ENV=production /usr/bin/ruby ….

4 Trackbacks/Pingbacks

  1. By Rails Snippets on 25 Feb 2009 at 10:12 am

    […] small snippets of code or links that I do like to remember during everyday work…I found: How to Execute a Rails Controller Action via CronTweaking it a little bit resp. using it in my code (from a sub directory $RAILS_HOME/tools)require […]

  2. By Rails Snippets on 25 Feb 2009 at 10:28 am

    […] I found an interesting site on the Web:  […]

  3. […] あとおまけとしてRunnerScriptでコントローラを使う方法がここにあった。 […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: