rails: "rails runner" can't be used in shebang lines

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/4249 Created by Andreas Mayer - 2011-02-19 09:28:16 UTC

In Rails 2.3, I used the following line as shebang line for my scripts that should run in a Rails environment: #!/usr/bin/env /path/to/my/app/script/runner

so exec(“/usr/bin/env”, “/path/to/my/app/script/runner”, “./my-script.rb”) was called and it worked.

In Rails 3.0.0beta1, the script/runner has been merged into the rails tool and as I understand it, I should call it as follows: $ rails runner ./my-script.rb which works.

But, when I want to use the runner as a shebang line (as “rails runner -h” tells me: You can also use runner as a shebang line for your scripts like this: ------------------------------------------------------------- #!/usr/bin/env /path/to/my/app/script/rails Product.find(:all).each { |p| p.price *= 2 ; p.save! } ------------------------------------------------------------- ) it doesn’t work because:

  1. The given shebang from rails runner tells me: $ ./my-script.rb Error: Command not recognized Usage: rails COMMAND [ARGS] …

because “rails” alone is not enough, it should be “rails runner”

  1. I can’t use “#!/usr/bin/env /path/to/my/app/script/rails runner” because parameters don’t get separated in shebang lines, so exec(“/usr/bin/env”, “/path/to/my/app/script/rails runner”, “./my-script.rb”); is called which returns $ ./my-script.rb /usr/bin/env: /path/to/my/app/script/rails runner: file or directory not found

  2. So I would have to use #!/path/to/my/app/script/rails runner the the parameters are correct but shebangs doesn’t execute scripts => env is needed

So my only possibility would be a wrapper script, i.e. I would have to re-write script/runner that calls “rails runner” or to always call “rails runner script.rb” instead of “./script.rb” directly.

So:

  1. Please fix the output of “rails runner -h”. The suggested shebang doesn’t work.
  2. Maybe re-introduce script/runner?

About this issue

  • Original URL
  • State: closed
  • Created 13 years ago
  • Comments: 30 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I believe this works and is portable:

#!/usr/bin/env ruby

if not defined?(Rails) then
  exec("rails", "runner", __FILE__, *ARGV)
end

If you use

#!/usr/bin/env ruby

if not defined?(Rails) then
  exec("rails", "runner", File.expand_path(__FILE__), *ARGV)
end

you can run from directories in the Rails subtree other than the Rails root directory.