比rufus/scheduler更独立简洁的定时脚本必备whenever

Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.

https://github.com/javan/whenever

 (1)首先安装这个gem

$ gem install whenever

(2)将这个gem添加到Gemfile里

Or with Bundler in your Gemfile.

gem 'whenever', :require => false

(3)运行以下命令,This will create an initial config/schedule.rb file for you.

$ wheneverize .

 

(4)编辑定时任务schedule.rb:

every 1.day, at: '00:01 am' do
  command "cd /opt/app/ruby/cms/current && RAILS_ENV=#{@environment} bundle exec rails runner script/fetch_video.rb", output: 'log/fetch.log'
end
以上含义是在每天晚上12:01执行脚本,

(5)想以contab的形式查看schedule.rb脚本,执行以下命令
$ whenever
 
 
19:23 [root@a03.test.3g.b28.youku]$ whenever --help
Usage: whenever [options]
    -i [identifier],                 Default: full path to schedule.rb file
        --update-crontab
    -w, --write-crontab [identifier] Default: full path to schedule.rb file
    -c, --clear-crontab [identifier]
    -s, --set [variables]            Example: --set 'environment=staging&path=/my/sweet/path'
    -f, --load-file [schedule file]  Default: config/schedule.rb
    -u, --user [user]                Default: current user
    -k, --cut [lines]                Cut lines from the top of the cronfile
    -r, --roles [role1,role2]        Comma-separated list of server roles to generate cron jobs for
    -v, --version

更新写入crontab

$whenever -i

在script里的脚本可以只写功能语句,好处就是与执行时间分离开了,更加达到解偶的效果.

(6)如果想立即执行脚本,也不需要去该fetch_video.rb里的代码,直接运行命令:
RAILS_ENV=production rails runner script/fetch_videos.rb 
本地环境可以省略前面的RAILS_ENV配置,因为默认就是development环境

(7)部署的时候需要注意在  "config/deploy.rb" file加上:
require "whenever/capistrano"
set :whenever_command, "bundle exec whenever"
(8)最后,部署完在服务器上查看一下
$ crontab -l

 















转载于:https://www.cnblogs.com/iwangzheng/p/3849615.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值