loosecannon
loosecannon

Reputation: 7803

have a relative shebang line

I'm writing a rails app and need to run come scripts through ./script/runner

while i could put

#!/home/cannon/src/timetracker/script/runner 

at the top, that wont work in production where it needs to be more like

#!/var/www/loclahost/htdocs/timetracker/script/runner -e=production

since ./script is not in my path, and I don't want it to be, how can I allow this to be set up,

I am using a cron job to run it on a Linux box

Upvotes: 5

Views: 2072

Answers (3)

Andy Triggs
Andy Triggs

Reputation: 1305

Now that script/runner is gone and Rails 7 uses "rails runner", there are problems using that in a shebang line with env because env doesn't parse spaces and looks for 'rails runner'.

This is the most elegant solution I found:

#!/usr/bin/env ruby

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

... Rails code ...

It's from https://github.com/rails/rails/issues/665.

Upvotes: 1

Use this at the top of your (ruby) script to re-exec it under the local ./script/runner (which should then define Rails so you avoid an infinite loop)

exec("./script/runner",$0,*ARGV) unless defined?(Rails)

(So use a regular 'ruby' shebang at the top, whether it's #!/usr/bin/ruby or #!/usr/bin/env ruby or whatever the flavor)

Upvotes: 0

Josh Lee
Josh Lee

Reputation: 177775

Use env in the shebang line to look things up in the path:

#!/usr/bin/env ./script/runner

Upvotes: 9

Related Questions