This project is archived and is in readonly mode.
[rails3 beta] "rails runner" can't be used in shebang lines
Reported by Andreas Mayer | March 21st, 2010 @ 11:23 PM | in 3.0.6
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?
Comments and changes to this ticket
-
Andreas Mayer March 21st, 2010 @ 11:26 PM
- Title changed from "rails runner" can't be used in shebang lines to [rails3 beta] "rails runner" can't be used in shebang lines
-
Yehuda Katz (wycats) March 27th, 2010 @ 07:53 AM
- Milestone cleared.
- Tag changed from rails 3.0 beta, runner to rails3, runner
- Assigned user set to Yehuda Katz (wycats)
-
Yehuda Katz (wycats) March 27th, 2010 @ 07:53 AM
- State changed from new to open
-
Rohit Arondekar June 22nd, 2010 @ 06:00 AM
Yehuda, will you be working on this? There is this one more ticket #2244 and I was about to write some tests, I'm guessing I should wait for changes caused by this ticket?
-
Rohit Arondekar June 22nd, 2010 @ 08:52 AM
... 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.
-
raggi June 22nd, 2010 @ 03:16 PM
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 atwhich ruby
. -
raggi June 22nd, 2010 @ 03:18 PM
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?
-
Jeremy Kemper August 30th, 2010 @ 04:10 AM
- Milestone cleared.
- Importance changed from to Low
-
Rohit Arondekar September 16th, 2010 @ 05:08 AM
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 https://rails.lighthouseapp.com/projects/8994/tickets/5637
Till a wrapper is introduced I think it's a good idea to fix the help output to something that's not entirely wrong.
-
Rohit Arondekar September 17th, 2010 @ 10:35 AM
- Assigned user changed from Yehuda Katz (wycats) to José Valim
-
Repository September 18th, 2010 @ 07:50 PM
(from [76266a818449c732440e7e2ef4de8442ac6af891]) Fix output of 'rails runner --help' [#4249 state:open] http://github.com/rails/rails/commit/76266a818449c732440e7e2ef4de84...
-
Rohit Arondekar September 19th, 2010 @ 08:34 AM
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.
-
Santiago Pastorino November 11th, 2010 @ 03:18 AM
Hey Rohit which is the state of this issue?. Needs something else?
-
Rohit Arondekar November 12th, 2010 @ 02:41 AM
- 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.
-
Rohit Arondekar January 27th, 2011 @ 04:51 AM
I tried 3 ways of going about this —
1] Moving the
rails runner
command code intoscript/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 withinscript/runner
using shell scriptI 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 withinscript/runner
using RubyThis 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.
-
Rohit Arondekar January 27th, 2011 @ 04:54 AM
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. -
Rohit Arondekar January 28th, 2011 @ 10:41 AM
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.
-
Rohit Arondekar January 28th, 2011 @ 10:59 AM
I'm going to just attach the patch which creates a wrapper for rails runner — basically the script just executes rails runner command.
-
Andreas Mayer January 29th, 2011 @ 01:16 PM
@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.
-
Rohit Arondekar January 29th, 2011 @ 01:20 PM
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?
-
Andreas Mayer January 29th, 2011 @ 01:57 PM
@Rohit: Thank you. As far as I see, this should work, but I haven't tested yet.
-
Rohit Arondekar February 1st, 2011 @ 05:04 AM
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 :)
-
José Valim February 1st, 2011 @ 09:36 AM
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.
-
Santiago Pastorino February 27th, 2011 @ 03:15 AM
- Milestone changed from 3.0.5 to 3.0.6
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 5637 rails runner --help documentation is off Duplicate #4249 Note that the ticket also includes fixin...
- 4249 [rails3 beta] "rails runner" can't be used in shebang lines (from [76266a818449c732440e7e2ef4de8442ac6af891]) Fix out...
- 5661 Rails runner help message is sent to stderr instead of stdout This happened because the patch in #4249 didn't get appl...