rails 使用 whenever

rails whenever使用

参考:

https://github.com/javan/whenever作者:javanjavan/whenever

http://asciicasts.com/episodes/164-cron-in-rubyCroninRuby

whenever用于提供用户的定时任务,在linux下写crontab

1.安装

$geminstallwhenever

或在Gemfile中添加

gem'whenever',:require=>false

2.在项目目录下

$cd~/workspace/deploy

$wheneverize.

输出:

[add]writing`./config/schedule.rb'

[done]wheneverized!

即创建config/schedule.rb配置文件

3.在schedule.rb中写入自己需要定时运行的任务:

如:

every1.daydo

rake"log:clear"

end

4.运行whenever命令,查看生成的crontab命令

$whenever

=>00***/bin/bash-l-c'cd/home/user01/workspace/deploy&&RAILS_ENV=productionrakelog:clear--silent'

可以看到默认环境为生产环境

5.写入到crontab中:

$whenever-senvironment=development-w/home/user01/workspace/deploy/config/schedule.rb

=>[write]crontabfilewritten

其中environment后为项目运行环境,-w后为schedule.rb的默认路径

6.更新crontab

$whenever-i

7.查看crontab

$crontab-l

=>
Begin Whenever generated tasks for: /home/user01/workspace/deploy/config/schedule.rb
0 0 * * * /bin/bash -l -c 'cd /home/user01/workspace/deploy && RAILS_ENV=production rake log:clear --silent'
# End Whenever generated tasks for: /home/user01/workspace/deploy/config/schedule.rb

8.清除crontab为:

$whenever-c

相关推荐