zoukankan      html  css  js  c++  java
  • 比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














  • 相关阅读:
    react中refs的使用
    在npm发布自己造的轮子
    如何阅读一本书——分析阅读模板
    如何阅读一本书——检视阅读模板
    Redis教程——检视阅读
    如何阅读一本书——分析阅读Pre
    SVN常用功能介绍(二)
    SVN常用功能介绍(一)
    分页sql大全
    .NetCore 登录(密码盐+随机数)
  • 原文地址:https://www.cnblogs.com/iwangzheng/p/3849615.html
Copyright © 2011-2022 走看看