Bundle exec rake不在cron中运行

Vit*_*ali 2 ruby ubuntu cron ruby-on-rails

我做了一些研究,但所有与cron和bundle exec相关的问题都没有解决,如果已经讨论过,我将再次为我辩解。

我正在运行Ubuntu 13.10,并且有一个Ruby On Rails应用程序,该应用程序几乎不需要每隔几分钟就在Cron上运行的一些rake任务。

我运行一个每当gem时,借助该语法

every 3.minutes do
  rake 'update_balance'
end
Run Code Online (Sandbox Code Playgroud)

转换为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'
Run Code Online (Sandbox Code Playgroud)

当我完全复制此行时

/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'
Run Code Online (Sandbox Code Playgroud)

并在它的控制台中运行,它运行得很好,并且可以按预期更新数据库中的多个记录。

但是当设置为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')
Run Code Online (Sandbox Code Playgroud)

即使我将&> / 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
Run Code Online (Sandbox Code Playgroud)

非常感谢对此问题的任何帮助。谢谢。

Max*_*ams 5

由于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
Run Code Online (Sandbox Code Playgroud)

  • 如果愿意,也可以[在crontab本身中设置环境变量](http://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-c​​rontab-will-use)它们适用于所有任务。 (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' (2认同)