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

时间:2021-08-10 12:19:07

I am using a shell script to run some runner scripts in my Ruby on Rails app. I need to run it on the production database, but the following:

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

#!/bin/bash
/usr/bin/ruby RAILS_ENV=production ../script/runner ../lib/tasks.rb 

gives an error:

给出了一个错误:

/usr/bin/ruby: No such file or directory -- RAILS_ENV=production (LoadError)

I have tried to force it in config/environment.rb

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

ENV['RAILS_ENV'] ||= 'production'

or even

甚至

ENV['RAILS_ENV'] = 'production'

but even with that it still runs in development environment.

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

Update: I can force the scripts to connect to the right database by editing the config/database.yml file, but I wonder what's the proper way of doing it.

更新:我可以通过编辑配置/数据库来强制脚本连接到正确的数据库。yml文件,但是我想知道怎么做才合适。

5 个解决方案

#1


25  

The help on the command line for script/runner gives you your answer.

脚本/运行程序的命令行上的帮助给出了您的答案。

script/runner -e production Model.method

#2


5  

If that's your command, the order of your arguments is your biggest problem.

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

/usr/bin/ruby RAILS_ENV=production ../script/runner ../lib/tasks.rb

Is different than.

是不同的。

/usr/bin/ruby ../script/runner ../lib/tasks.rb RAILS_ENV=production

The second example is looking for the file, the first one is setting a runtime variable while ruby interpreting it as the file you want to run.

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

#3


5  

If you redo your script like this:

如果你像这样重写剧本:

#!/bin/bash
RAILS_ENV=production
/usr/bin/ruby ../script/runner ../lib/tasks.rb

...that will make it stick for the lifetime of the script. To make it stick for the lifetime of the shell's session, change it to

…这将使它在脚本的整个生命周期中都保持不变。要使它在shell会话的生命周期内保持稳定,请将其更改为

#!/bin/bash
export RAILS_ENV=production
/usr/bin/ruby ../script/runner ../lib/tasks.rb

#4


1  

You can set the environment variable like this :

您可以这样设置环境变量:

RAILS_ENV=production /usr/bin/ruby ../script/runner ../lib/tasks.rb

#5


0  

RAILS_ENV=production script/rails runner 'user = User.find(:first, :conditions => {:admin => true}) ; user.password, user.password_confirmation = "mypasswd"; user.save!'

it worked for me

它为我工作

#1


25  

The help on the command line for script/runner gives you your answer.

脚本/运行程序的命令行上的帮助给出了您的答案。

script/runner -e production Model.method

#2


5  

If that's your command, the order of your arguments is your biggest problem.

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

/usr/bin/ruby RAILS_ENV=production ../script/runner ../lib/tasks.rb

Is different than.

是不同的。

/usr/bin/ruby ../script/runner ../lib/tasks.rb RAILS_ENV=production

The second example is looking for the file, the first one is setting a runtime variable while ruby interpreting it as the file you want to run.

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

#3


5  

If you redo your script like this:

如果你像这样重写剧本:

#!/bin/bash
RAILS_ENV=production
/usr/bin/ruby ../script/runner ../lib/tasks.rb

...that will make it stick for the lifetime of the script. To make it stick for the lifetime of the shell's session, change it to

…这将使它在脚本的整个生命周期中都保持不变。要使它在shell会话的生命周期内保持稳定,请将其更改为

#!/bin/bash
export RAILS_ENV=production
/usr/bin/ruby ../script/runner ../lib/tasks.rb

#4


1  

You can set the environment variable like this :

您可以这样设置环境变量:

RAILS_ENV=production /usr/bin/ruby ../script/runner ../lib/tasks.rb

#5


0  

RAILS_ENV=production script/rails runner 'user = User.find(:first, :conditions => {:admin => true}) ; user.password, user.password_confirmation = "mypasswd"; user.save!'

it worked for me

它为我工作