whenever

GitHub - javan/whenever: Cron jobs in Ruby
CronjobsinRuby.Contributetojavan/wheneverdevelopmentbycreatinganaccountonGitHub.

定期的にバッチ処理をさせる。cronを簡単に使えるようにするgem。

gemfileに以下を追記してbundle install

gem 'whenever', require: false

使い方

設定ファイルを作成

bundle exec wheneverize .

dotが付いてるのを忘れずに。
実行されるとschedule.rbが作成されます。

[add] writing `./config/schedule.rb'
[done]  wheneverized!

実行したい内容を記載

config/schedule.rbに以下のような感じで実行内容を記載。

every 2.hours do
  command "/usr/bin/my_command"
  runner "Model.method"
  rake "something:rake:task"
end
commandbashの実行
rakerakeの実行
runnerrailsメソッドの実行
scriptscriptの実行

optionの確認

whenever -hで確認できる。

$whenever -h
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
    -x, --crontab-command [command]  Default: crontab
    -v, --version

crontabを確認して設定

設定を確認。
以下のコマンドはscadule.rbをcrontabに変換して表示するのみで、書き込みも保存もしない。

bundle exec whenever

設定を反映するにはこっちを実行。

bundle exec whenever --update-crontab

設定を削除する場合はこちら。

bundle exec whenever --clear-crontab

cronのログ

Macの場合は自動的にはログを出力しないようになっている。
どうやってやるの?以下の記事を参考にしたけどログが出ない。

Where is the cron log file in MacOSX Lion?
Iwanttotroubleshootacronjobthatworkedfineuntilarecentmodification,butIcan'tfindthecronlogfile,whereisit?

コメント