我正在将 whenever gem 与 Rails 3 一起使用。在我的生产服务器(ubuntu)上,运行程序任务不会跑步。我尝试设置 :set job_template
以获取 -l -i
如 此 github 票证。但这并不能解决问题。
这个特定生产 ubuntu 的问题是 echo $PATH
中不存在 ruby 路径:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
而 ruby 路径是 /var/rails/myapp/shared/bundle/ruby/1.8/bin
所以如果我手动编辑 crontab 文件并添加 PATH=/var/rails/myapp/shared/bundle/ruby/1.8/bin:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games 到 crontab 文件,runner 任务正确执行。
但是,每次进行部署时,我都需要手动编辑 crontab 文件以向其中添加 PATH 语句。
有没有什么方法可以在 crontab 文件中添加此 PATH 行,这样每次部署后就不需要手动执行此操作?
谢谢
I am using whenever gem with rails 3. On my production server (ubuntu) , the runner task does not run. I tried setting the :set job_template
to get -l -i
as mentioned in this github ticket. However that does not solve the problem.
The problem on this particular production ubuntu is that the ruby path is not there in echo $PATH
:
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Whereas the ruby path is /var/rails/myapp/shared/bundle/ruby/1.8/bin
So if I manually edit the crontab file and add PATH=/var/rails/myapp/shared/bundle/ruby/1.8/bin:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
to the crontab file, the runner task is executed correctly.
However every time I do a deploy, I need to manually edit the crontab file to add the PATH statement to it.
Is there any way in whenever to add this PATH line in crontab file, so that there would not be any need to do this manually after every deploy?
Thanks
发布评论
评论(4)
我没有使用 RVM 并在文件 config/schedule.rb 中添加以下代码(每当 gem 相关代码时您编写的位置)对我有用。
I am not using RVM and adding the below code in the file config/schedule.rb(place where u write whenever gem related code) worked for me.
我认为如果您将 /var/rails/myapp/shared/bundle/ruby/1.8/bin 添加到服务器上运行的任何用户 cron 的 PATH 中,则应该选择它。或者,您可以将其添加到每当schedule.rb中:
应该可以解决问题,但我还没有测试过它。
I think if you add /var/rails/myapp/shared/bundle/ruby/1.8/bin to the PATH of whatever user cron is running under on the server, it should be picked up. Or, you could add it in the whenever schedule.rb:
That should do the trick, but I haven't tested it.
idlefingers 的答案看起来大部分是正确的,但根据 ami 的评论,我会将该行更改为以下内容:
注意 PATH 环境键的字符串插值。我还没有对此进行测试,但根据 ami 的评论,这应该按预期完全扩展 crontab 文件中的路径字符串。
The answer from idlefingers looks mostly correct, but based on the comment from ami, I would change that line to the following:
Notice the string interpolation for the environment key for PATH. I have not tested this, but based on ami's comment, this should fully expand the path string in the crontab file as expected.
将 PATH 语句添加到 crontab 文件的顶部、开始行之前
,这样您就不必每次都手动编辑 crontab 文件
Add the PATH statement to the top of the crontab file, before the line that starts
and you shouldn't have to manually edit your crontab file every time