1、在本地项目文件夹中的Gemfile中添加rake、whenever的gem,并执行
bundle install
,
git push
推送服务器
2、在lib/tasks文件夹中添加.rake文件,.rake文件的内容就是你要执行的代码,保存完rake文件后,git push
推送服务器
示例:
namespace :update_password do
desc "force_update_password_periodically"
task :force_update_password => :environment do
puts "force_update_password"
end
end
3、在本地或者已部署的服务器上,执行
bundle exec rake update_password:force_update_password
测试代码是否可用
4、在本地执行bundle exec whenever
命令,使得项目中config文件夹中生成schedule.rb文件,git push推送服务器
5、在本地schedule.rb文件,中添加配置执行.rake文件的代码,git push推送服务器
示例:
set :environment, :development
set :output, 'log/cron.log'
every '30 9 * * *' do
rake "update_password:force_update_password"
end
6、在本地项目中执行bundle exec whenever --update-crontab
,使得schedule.rb中的添加的定时任务添加到当前机器的crontab列表中,测试没有问题之后,就可以在远端服务器上执行bundle exec whenever --update-crontab
,使得新添加的rake任务添加到当前机器的crontab列表中