Kar*_*amy 7 ruby cron ruby-on-rails crontab
我试图在Rails 3.0.9(使用RVM)中使用crontab运行Rake任务,但它无法正常工作.
但是当我在控制台中运行时它工作正常
我的耙子任务
namespace :alert do
desc "create some reminder notification"
task :send_reminder => :environment do
p "my task goes here ----"
end
end
Run Code Online (Sandbox Code Playgroud)
我的cron任务
*/1 * * * * cd /home/anu-karthik/Documents/billguru/ && /home/anu-karthik/.rvm/gems/ruby-1.9.2-p0/bin/rake alert:send_reminder >/home/anu-karthik/alert.out
Run Code Online (Sandbox Code Playgroud)
但我没有在"alert.out"文件中找到任何日志条目
我也试过以下方法
*/1 * * * * cd /home/anu-karthik/Documents/billguru/ && rake alert:send_reminder >/home/anu-karthik/alert.out
Run Code Online (Sandbox Code Playgroud)
现在输出是(在/ home/anu-karthik/Documents/billguru)
我认为这是RVM的问题.我该如何解决这个问题?提前致谢
Tan*_*a R 12
根据RVM文档:https://rvm.io/integration/cron
对于每个rvm或gemset,都有一个描述它的环境文件.你可以用以下方式获得它:
rvm env --path -- ruby-version[@gemset-name]
Run Code Online (Sandbox Code Playgroud)
这是rvm环境文件的路径.
从cron运行rake任务,从shell脚本调用它们是一个好习惯.
crontab应该是这样的:
*/30 * * * * /path/to/shell/script.sh >/dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)
然后在shell脚本中:
#!/bin/bash
cd /path/to/project
source /path/to/env/file
rake task
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
尝试使用gem来编写和部署您的 cron 作业。
默认情况下,所有作业均使用 bash -l -c 'command...' 运行。除此之外,这允许您的 cron 作业通过加载整个环境而不是 cron 的有些有限的环境来与 RVM 很好地配合。了解更多:http ://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-product
从描述来看应该对你有帮助。