2014-05-13 119 views
2

我做了一些研究,但是所有與cron和bundle exec有關的問題都沒有涵蓋如果討論過這個問題,我還有這麼多問題。bundle exec rake不能在cron中運行

我正在運行Ubuntu 13.10,並且每隔幾分鐘左右就有一個Ruby On Rails應用程序需要在Cron上運行少量rake任務。

我跑每當寶石,與這句法

every 3.minutes do 
    rake 'update_balance' 
end 

轉變這條線在crontab文件幫助

0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent' 

當我複製此行正是

/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent' 

並在其中運行控制檯,它運行得非常好,並更新數據庫中的幾個記錄,如suppos編輯。

但是,當設置爲cron時,我可以看到它在/ var/log/syslog文件中運行,但沒有任何執行。

May 13 13:06:01 sandbox2 CRON[9656]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent') 
May 13 13:06:01 sandbox2 CRON[9655]: (CRON) info (No MTA installed, discarding output) 
May 13 13:09:01 sandbox2 CRON[9789]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent') 

即使當我添加&> /tmp/mycommand.log到crontab命令,每一次啓動cron命令的只是完全截斷這個文件,但再次,如果我手動啓動它,它工作得很好,並給我留下與此輸出。

2014-05-13T11:11:25Z 10292 TID-2asbo INFO: Sidekiq client with redis options {:url=>"redis://127.0.0.1"} 
Sent task for updating 2 users 

對這個問題的任何幫助是非常感謝。 謝謝。

回答

4

由於cron作爲一個不同的用戶運行,我遇到過這樣的問題。特別是使用rake時,我必須使用rake的完整路徑,因爲cron用戶在PATH中沒有正確的文件夾。

所以,我對耙任務的cron線是這樣的:

30 8 * * 1 cd /ebs/www/apps/myproject/www && /usr/local/bin/rake mailer:send_weekly_expiring_users_reminder RAILS_ENV=production 
+2

您也可以[中設置環境變量的'crontab'本身(http://stackoverflow.com/questions/2229825/where -can-i-set-environment-variables-that-c​​rontab-will-use)如果你想讓它們適用於所有任務。 – tadman

+0

這聽起來更合理:) –

+2

謝謝,它的工作。以後再看看環境變量。 這是我的crontab行現在看起來如何。 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * */bin/bash - l -c'cd/var/fruby/releases/20140513091404 && RAILS_ENV = production/usr/local/bin/bundle exec/usr/bin/rake update_balance --silent' – Screatch

相關問題