2011-11-08 106 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中使用腳本文件而不是單行命令可以更容易管理,您也可以在腳本文件中使用上述命令。