Ruby on Rails:如何指定runner脚本环境

Gor*_*oro 12 linux bash ruby-on-rails

我正在使用shell脚本在我的Ruby on Rails应用程序中运行一些运行程序脚本.我需要在生产数据库上运行它,但以下内容:

#!/bin/bash
/usr/bin/ruby RAILS_ENV=production ../script/runner ../lib/tasks.rb 
Run Code Online (Sandbox Code Playgroud)

给出错误:

/usr/bin/ruby: No such file or directory -- RAILS_ENV=production (LoadError)
Run Code Online (Sandbox Code Playgroud)

我试图在config/environment.rb中强制它

ENV['RAILS_ENV'] ||= 'production'
Run Code Online (Sandbox Code Playgroud)

甚至

ENV['RAILS_ENV'] = 'production'
Run Code Online (Sandbox Code Playgroud)

但即便如此,它仍然在开发环境中运行.

更新:我可以通过编辑config/database.yml文件强制脚本连接到正确的数据库,但我想知道这样做的正确方法是什么.

nit*_*der 27

脚本/跑步者命令行的帮助为您提供答案.

script/runner -e production Model.method
Run Code Online (Sandbox Code Playgroud)


Gar*_*ett 5

如果这是你的命令,你的论点的顺序是你最大的问题.

/usr/bin/ruby RAILS_ENV=production ../script/runner ../lib/tasks.rb
Run Code Online (Sandbox Code Playgroud)

不同于.

/usr/bin/ruby ../script/runner ../lib/tasks.rb RAILS_ENV=production
Run Code Online (Sandbox Code Playgroud)

第二个示例是查找文件,第一个是设置运行时变量,而ruby将其解释为您要运行的文件.


Ave*_*yne 5

如果你像这样重做你的脚本:

#!/bin/bash
RAILS_ENV=production
/usr/bin/ruby ../script/runner ../lib/tasks.rb
Run Code Online (Sandbox Code Playgroud)

...这将使它在脚本的生命周期中坚持下去.要使它坚持shell会话的生命周期,请将其更改为

#!/bin/bash
export RAILS_ENV=production
/usr/bin/ruby ../script/runner ../lib/tasks.rb
Run Code Online (Sandbox Code Playgroud)