最近我遇到了以下运行cron作业而不使用任何Gems的教程
http://www.ameravant.com/posts/recurring-tasks-in-ruby-on-rails-using-runner-and-cron-jobs
我在/app/delete_old_posts.rb中创建了一个文件
class DeleteOldPosts < ActiveRecord::Base
# This script deletes all posts that are over 5 minutes old
post_ids = Post.find(:all, :conditions => ["created_at < ?", 5.minutes.ago])
if post_ids.size > 0
Post.destroy(post_ids)
puts "#{post_ids.size} posts have been deleted!"
end
然后通过给crontab -e命令创建cron作业,并在我编写的cronjob控制台中创建
*/2 * * * * /usr/bin/env ruby /home/abc/xyz/urjit.rajgor/workspace/thewall/rails/runner/home/XYZ/ABC/urjit.rajgor/workspace/thewall/app/delete_old_posts.rb
cron工作每两分钟运行一次但是没有用
请帮我 .
谢谢
1 回答
尝试使用“随时”宝石 . 允许您在ruby中定义cronjobs,可以指定rails runner,rake或其他自定义包装器,它可以为您编写crontab . 让生活更简单 .
只需将:
gem 'whenever', :require => false
添加到您的gemfile中https://github.com/javan/whenever
http://railscasts.com/episodes/164-cron-in-ruby