简体   繁体   English

如何使用CRON定期运行rake任务?

[英]How to run a rake task regularly with using CRON?

I've written a rake task for my Rails application and I'd need to run this tasks regularly with using CRON. 我已经为Rails应用程序编写了一个rake任务,并且需要使用CRON定期运行此任务。

If I have a URL that I need to ping with CRON, I do it like this: 如果我有一个需要与CRON一起ping的URL,则可以这样操作:

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

But how to "ping" a rake task? 但是如何“ ping”耙任务?

The application is located in /home/deployer/apps/myapp-production/current and is running on DigitalOceal (Ubuntu server - nginx). 该应用程序位于/home/deployer/apps/myapp-production/current并在DigitalOceal(Ubuntu服务器-nginx)上运行。

Thanks. 谢谢。

EDIT: This is my command: 编辑:这是我的命令:

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

But the output is: 但是输出是:

/bin/sh: 1: bundle: not found

When I run just rake db:backup on my laptop (locally), everything works just well. 当我在笔记本电脑上(仅在本地)运行rake db:backup时,一切正常。

Do I have incorrect the path in the CRON task? CRON任务中的路径是否不正确?

EDIT2: When I run the command cd /home/deployer/apps/myapp-production/current && RAILS_ENV=production bundle exec rake db:backup manually from the command line, everything is working, but not from the CRON. EDIT2:当我从命令行手动运行命令cd /home/deployer/apps/myapp-production/current && RAILS_ENV=production bundle exec rake db:backup ,一切正常,但未从CRON运行。

Use whenever gem. 使用每当宝石。 It provides nice DSL like: 它提供了不错的DSL,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM