2011-11-08 102 views
3

我试图设置运行rake任务的cron作业。每当我使用宝石。这里是Cron运行rake任务“无法在任何源中找到rake-0.8.7”

every 1.minutes do 
    bundle exec rake "test:pick_participant" 
end 

crontab中无论何时,只要正确设置在配置/ schedule.rb配置:

* * * * * /bin/bash -l -c 'cd /home/jsmith/webapp/releases/20111104200246 && RAILS_ENV=production bundle exec rake test:pick_participant --silent' 

然而,克龙不断报告(通过电子邮件)这个错误每次工作是跑:

From: [email protected] (Cron Daemon) 
To: [email protected] 
Subject: Cron <[email protected]> /bin/bash -l -c 'cd /home/jsmith/webapp/releases/20111104200246 && RAILS_ENV=production bundle exec rake test:pick_participant --silent' 
Content-Type: text/plain; charset=ANSI_X3.4-1968 
X-Cron-Env: <[email protected]> 
X-Cron-Env: <SHELL=/bin/sh> 
X-Cron-Env: <HOME=/home/jsmith> 
X-Cron-Env: <PATH=/usr/bin:/bin> 
X-Cron-Env: <LOGNAME=jsmith> 
Message-Id: <[email protected]> 
Date: Mon, 7 Nov 2011 16:26:01 -0800 (PST) 
expr: syntax error 
Could not find rake-0.8.7 in any of the sources 

捆绑在应用程序环境中的rake版本是0.8.7。

由cron发出的命令似乎是正确的:

/bin/bash -l -c 'cd /home/jsmith/webapp/releases/2067320376 && RAILS_ENV=production bundle exec rake test:pick_participant --silent' 

它直接调用bundle exec rake代替rake

另外,如果我在发布的应用程序目录中的命令行相同的命令,rake任务运行成功:

[email protected]:~/webapp/current$ /bin/bash -l -c 'cd /home/jsmith/webapp/releases/20111104200246 && RAILS_ENV=production bundle exec rake test:pick_participant --silent' 

* picked participant: Mindy! 

任何人有,为什么cron是遇到了此问题的任何想法“找不到耙0.8.7" ?

回答

7

我已经解决了这个问题。

有两个问题,作为报道的Cron错误输出:

expr: syntax error 
Could not find rake-0.8.7 in any of the sources 

这是一些谷歌的搜索和调试后,我如何修复这些问题:

  1. exp: syntax error是针对我Ubuntu 10.4 LTS安装。为了解决这个问题,该文件/etc/profile.d/speechd-user-port.sh中,与

    [ "$PS1" != "" ] && export SPEECHD_PORT=$(expr 6560 + $(getent passwd $USER | cut -f 3 -d :)) 
    

    变量扩展$ USER导致语法错误更换线

    export SPEECHD_PORT=$(expr 6560 + $(getent passwd $USER | cut -f 3 -d :)) 
    

    。这是Ubuntu 10.4语音调度程序包中的一个错误。见详细说明这里:语法错误已被固定

    Ubuntu bug#790173 Cron doesn't send output properly Comment 6

    Ubuntu bug#601114 /etc/profile.d/speechd-user-port.sh references $USER

  2. 后,我继续得到Could not find rake-0.8.7 in any of the sources错误。

    这是由于RVM未加载到我的登录会话中。该行源RVM功能

    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" 
    

    只有在〜/ .bashrc,和〜/ .bash_profile中

    为交互式非登录shell和〜/加载文件〜/ .bashrc。bash_profile是为登录shell加载的,这是在Cron作业命令“/ bin/bash -l”中加载的开关。 RVM website明确规定将该行放入〜/ .bash_profile中。

    将行添加到〜/ .bash_profile,Cron作业成功加载RVM环境。现在,我的Cron rake任务顺利进行。

这里有一些东西,我在调试这个问题了解到:

  • cron作业环境是从登录shell的命令行不同。 Cron环境更加裸露,并没有像登录shell那样加载许多路径/配置文件。这就是为什么命令可能在命令行中工作,但不能作为Cron作业。

  • on Rails和耙切割出红宝石,我调试cron中运行的最简单的命令,用crontab项,如这些:

    * * * * * /bin/bash -l -c 'pwd’ 
    * * * * * /bin/bash -l -c 'echo $PATH’ 
    

    或者这些在每次的config/schedule.rb

    every 1.minutes do 
        rake "--version" 
        command "rvm info" 
    end 
    

    这些简单的命令让我意识到“expr:语法错误”是与Cron或RoR无关的基本事情,并且Rake错误是由于RVM未正确加载导致的。

  • 之间的差异.bashrc and .bash_profile

+0

感谢您在crontab中提及'/ bin/bash -l -c'。这也解决了我与捆绑商的问题。 – Roman

+0

很棒的回答。感谢您分享您的解决方案。 –

0

你总是可以只加载与科雷您的RVM环境,那么你应该能够运行任何rake任务,像这样:

0 * * * * /home/user/.rvm/bin/rvm use ree-1.8.7-2011.03 rake name:task 

哪里ree-1.8.7-2011.03是宝石的加载名称,类型rvm list看到你的宝石。在crontab中使用脚本文件而不是单行命令可以更容易管理,您也可以在脚本文件中使用上述命令。