Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[rails3 beta] "rails runner" can't be used in shebang lines

#4249

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"

2) 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

3) 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?

Reported by Andreas Mayer · March 21st, 2010 @ 11:23 PM

State: open
Milestone: 3.0.6
Assigned to: Rohit Arondekar Rohit Arondekar
Importance: Low

Activity

  1. Andreas Mayer
    Andreas Mayer
    • Title changed from "rails runner" can't be used in shebang lines to [rails3 beta] "rails runner" can't be used in shebang lines

    March 21st, 2010 @ 11:26 PM

  2. trevor
    trevor

    +1

    this is a big issue for me as well

    March 23rd, 2010 @ 12:28 AM

  3. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • Milestone cleared.
    • Tag changed from rails 3.0 beta, runner to rails3, runner
    • Assigned user set to Yehuda Katz (wycats)

    March 27th, 2010 @ 07:53 AM

  4. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to open

    March 27th, 2010 @ 07:53 AM

  5. Rohit Arondekar
    Rohit Arondekar

    Yehuda, will you be working on this? There is this one more ticket #2244 script runner should overwrite $0 and I was about to write some tests, I'm guessing I should wait for changes caused by this ticket?

    June 22nd, 2010 @ 06:00 AM

  6. Rohit Arondekar
    Rohit Arondekar

    ... because parameters don't get separated in shebang lines ...

    According to this Wikipedia entry it's a portability thing. Not all systems separate the arguments :(

    So building a wrapper might be the only option imho.

    June 22nd, 2010 @ 08:52 AM

  7. raggi
    raggi

    Yup, portability is a bitch with this, we have the same problem with rackup for cgis in rack. No ideal solution, I just recommend to people that they write a .cgi / .fcgi with a shebang directed at which ruby.

    June 22nd, 2010 @ 03:16 PM

  8. raggi
    raggi

    Maybe add a generator that makes a script/runner?

    That way it could be built by a rake task, rather than having a potentially non-working committed version?

    June 22nd, 2010 @ 03:18 PM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone cleared.
    • Importance changed from to Low

    August 30th, 2010 @ 04:10 AM

  10. Rohit Arondekar
    Rohit Arondekar

    Attached is a patch that fixes the --help output to include runner in the shebang line. Although it won't work on many systems it might still work on some as indicated here #5637 rails runner --help documentation is off

    Till a wrapper is introduced I think it's a good idea to fix the help output to something that's not entirely wrong.

    September 16th, 2010 @ 05:08 AM

  11. Rohit Arondekar
    Rohit Arondekar
    • Assigned user changed from Yehuda Katz (wycats) to José Valim

    September 17th, 2010 @ 10:35 AM

  12. Repository
  13. Rohit Arondekar
    Rohit Arondekar

    I think the patch should be applied to 3-0-stable too so that in 3.0.1 as a bare minimum the help message will be correct, in case the wrapper isn't ready by then.

    September 19th, 2010 @ 08:34 AM

  14. Jeremy Kemper
  15. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:34 AM

  16. Santiago Pastorino
    Santiago Pastorino

    Hey Rohit which is the state of this issue?. Needs something else?

    November 11th, 2010 @ 03:18 AM

  17. Rohit Arondekar
    Rohit Arondekar
    • Assigned user changed from José Valim to Rohit Arondekar

    Santiago, the original issue still remains. You can't use rails runner from shell scripts (in the shebang line) on certain platforms. I'll take a look at it again as soon as I can and try to work on a patch.

    November 12th, 2010 @ 02:41 AM

  18. Santiago Pastorino
  19. Rohit Arondekar
    Rohit Arondekar

    I tried 3 ways of going about this —

    1] Moving the rails runner command code into script/runner

    This is easy and I think I've gotten it working 100%. But it leads to duplication i.e there is a rails runner command and the same code in script/runner. We could maybe deprecate rails runner and in the next to next release remove it altogether.

    2] Running the rails runner command from within script/runner using shell script

    I don't know if I can write a general script that will work in bash, sh and other scripting environments. So I immediately stopped trying in this direction.

    3] Running the rails runner command from within script/runner using Ruby

    This looks like the most promising way and seems to work perfectly fine. I use the rails executable from app/script/rails since even the rails command uses it.

    I can work on a patch on either option 1 or option 2. I personally suggest option 3 since it reuses the rails runner command using just a 3 line ruby script. No duplication that comes with the first method.

    January 27th, 2011 @ 04:51 AM

  20. Rohit Arondekar
    Rohit Arondekar

    P.S the script for option 3 is as follows:

    #!/usr/bin/env ruby
    
    path_to_rails = File.dirname(File.expand_path(__FILE__))
    exec("#{path_to_rails}/rails runner #{ARGV[0]}")
    

    You can just drop it in your_rails_app/script/runner — chmod +x the file and use it in place of rails runner.

    January 27th, 2011 @ 04:54 AM

  21. Rohit Arondekar
    Rohit Arondekar

    I'm actually trying to find somebody who uses the runner as shebang lines in their scripts and I honestly can't find one person. If anybody following this ticket does use runner that way, could you give some feedback on the above wrapper approach? I don't know if there are any limitations of doing it that way.

    January 28th, 2011 @ 10:41 AM

  22. Rohit Arondekar
    Rohit Arondekar

    I'm going to just attach the patch which creates a wrapper for rails runner — basically the script just executes rails runner command.

    January 28th, 2011 @ 10:59 AM

  23. Andreas Mayer
    Andreas Mayer

    @Rohit: I did, and why shouldn't I? It is a documented feature and I didn't even have the idea that I couldn't run a Ruby script from crontab with a single command (so, using the shebang line).

    Of course, if it's documented that this doesn't work, it's also OK. I just want to be able to call method from my Rails project from crontab with one command.

    January 29th, 2011 @ 01:16 PM

  24. Rohit Arondekar
    Rohit Arondekar

    Andreas, could you give some feedback on the above patch? Does that do what you want? I'm sorry but I'm unsure about things as I myself don't use the rails runner. If the patch isn't satisfactory could you please work on one?

    January 29th, 2011 @ 01:20 PM

  25. Andreas Mayer
    Andreas Mayer

    @Rohit: Thank you. As far as I see, this should work, but I haven't tested yet.

    January 29th, 2011 @ 01:57 PM

  26. Rohit Arondekar
    Rohit Arondekar

    Andreas, please do test it. I'm finding it hard to find more people who can take a look at it actually. So if you know anybody else who might have an opinion please do direct them here! Thanks :)

    February 1st, 2011 @ 05:04 AM

  27. José Valim
    José Valim

    Please remove the help message from the script/rails runner output. I think it is easier for people to roll their own script then having a script/runner in rails mostly because using script/runner directly wouldn't work as you need to call bundle exec script/runner.

    February 1st, 2011 @ 09:36 AM

  28. Santiago Pastorino
  29. Santiago Pastorino
    Santiago Pastorino
    • Milestone changed from 3.0.5 to 3.0.6

    February 27th, 2011 @ 03:15 AM

  30. bingbing