最近,我看到了以下不使用任何Gem运行cron作业的教程
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作业每隔两分钟运行一次,但不起作用
请帮帮我。
谢谢
发布于 2012-04-18 00:35:42
试着使用“任何时候”这个gem。允许你在ruby中定义你的cronjob,能够指定rails runner,rake,或者其他定制的包装器,它会为你处理crontab的编写。让生活变得简单多了。
只需将:gem 'whenever', :require => false添加到您的gemfile
https://github.com/javan/whenever
http://railscasts.com/episodes/164-cron-in-ruby
https://stackoverflow.com/questions/10192257
复制相似问题