【问题标题】:How to run a rake task regularly with using CRON?如何使用 CRON 定期运行 rake 任务?
【发布时间】:2014-12-21 19:35:58
【问题描述】:

我为我的 Rails 应用程序编写了一个 rake 任务,我需要使用 CRON 定期运行此任务。

如果我有一个需要用 CRON ping 的 URL,我会这样做:

0 */6 * * * curl https://www.website.com/something

但是如何“ping”一个 rake 任务呢?

应用程序位于/home/deployer/apps/myapp-production/current,并在 DigitalOceal(Ubuntu 服务器 - nginx)上运行。

谢谢。

编辑:这是我的命令:

0 */6 * * * cd /home/deployer/apps/myapp-production/current && RAILS_ENV=production bundle exec rake db:backup

但是输出是:

/bin/sh: 1: bundle: not found

当我在笔记本电脑上(本地)运行 rake db:backup 时,一切正常。

我在 CRON 任务中的路径不正确吗?

EDIT2:当我从命令行手动运行命令 cd /home/deployer/apps/myapp-production/current && RAILS_ENV=production bundle exec rake db:backup 时,一切正常,但不是来自 CRON。

【问题讨论】:

  • 你要部署到heroku吗?
  • 不,正如 OP 中所述,它在 DigitalOcean 的服务器上运行。

标签: ruby-on-rails ruby cron rake rake-task


【解决方案1】:

使用whenever 宝石。它提供了很好的 DSL,例如:

every :day, :at => '12:20am', :roles => [:app] do
  rake "app_server:task"
end

【讨论】:

  • 感谢 Alexander 的回答,但是由于我已经使用 CRON 来处理项目中的一些其他任务,我也欢迎使用它来完成这个。
  • 不客气。它会根据 dsl 为你生成一个 crontask。像这样:0 22 * * * /bin/bash -l -c 'cd /srv/app/current && RAILS_ENV=production bundle exec rake your_rake'
  • 我会标记这个答案,因为它引导我成功解决了问题。
  • @user984621 你的实际解决方案是什么?
猜你喜欢
  • 2016-11-03
  • 2012-09-02
  • 2013-03-03
  • 2011-07-27
  • 2012-07-19
  • 2011-12-24
  • 2018-12-15
  • 2010-10-09
  • 2019-05-08
相关资源
最近更新 更多