Run Rails commands outside of console

20,962

Solution 1

There are two main ways to run commands outside console:

  1. Rake task which depends on :environment

  2. rails runner (previously script/runner), eg:

    $ rails runner "query"
    

Both are pretty well documented on the rails guide: https://guides.rubyonrails.org/command_line.html#bin-rails-runner

btw: both of these methods will still take the same time as a console to fire up, but they are useful for non-interative tasks.

Solution 2

Just pipe it in:

echo 'puts Article.count' | bundle exec rails c

It should now be a lot faster than when then the question was originally asked, because of Spring. It's not immediate, but still a lot faster than spinning up the whole app. Use this for the fast lane, it should run in under a second (assuming your required command is fast):

echo 'puts Article.count' | spring rails c

If you really want a single long-running process, you could easily do it by creating a controller action that simply runs whatever you POST to it, then send commands to it using curl behind an alias. The action would of course be completely insecure and should be triple-guarded against running anywhere near production, but it would be easy to setup.

Share:
20,962

Related videos on Youtube

tekknolagi
Author by

tekknolagi

(your about me is currently blank) click here to edit

Updated on October 09, 2021

Comments

  • tekknolagi
    tekknolagi over 2 years

    With my large application, the Rails console takes a while to load up. Is there a way to single commands more easily?

    I'd also like to be able to automate stuff, and echo "query" | rails console isn't a great way to do things.

    Thoughts?

    EDIT: What about a long-running process that I can ping queries to whenever I have need?

    • Andrew Marshall
      Andrew Marshall almost 12 years
      I'm curious what your use case for this is, as it seems that there's probably a better solution.
    • tekknolagi
      tekknolagi almost 12 years
      I'm using Geckoboard to grab stats from a running application, using a JSON feed I'm setting up with these queries... am I doing it completely wrong?
    • Andrew Marshall
      Andrew Marshall almost 12 years
      It would probably make more sense to just build this code right into your app and grab the data over HTTP. If it's sensitive info, then require some sort of authorization, e.g. an API key with the request.
    • tekknolagi
      tekknolagi almost 12 years
      I, unfortunately, don't have much experience at all with Rails. I'd have no idea how to build a query like User.all.count into a page and grab the results. How should I do that?
    • Andrew Marshall
      Andrew Marshall almost 12 years
      Well if you're trying to make a JSON feed, then you would just generate it as JSON. There are plenty of resources for how to send a JSON response using Rails.
    • tekknolagi
      tekknolagi almost 12 years
      How would I add it to the app though?
    • Andrew Marshall
      Andrew Marshall almost 12 years
      If you don't know how to do that, then perhaps you need to work through a Rails tutorial or the Rails Guides. It's a rather broad question.
  • Joshua Pinter
    Joshua Pinter over 5 years
    Genius!! Btw, if you're trying to return a "fail exit code" you use abort. For example: echo 'abort' | bundle exec rails c && echo 'Success!' will not show Success! in the console where as echo 'exit' | bundle exec rails c && echo 'Success!' will show Success!.