cronとは
unix系OS(CentOS、Ubuntuなど)で使えるプロセス(≒機能)。
主に決まった時間に定期実行したいプログラムを動かすために使う。
設定方法
$ crontab -e
viエディタが立ち上がるので、以下のルールに従って記述
分 時 日 月 曜日 ユーザー名 コマンド
* * * * * root some_command
指定対象 | 指定範囲 |
---|---|
分 | 0〜59 |
時 | 0〜23 |
日 | 1〜31 |
月 | 1〜12 または jan〜dec |
曜日 | 0〜7 または sun〜sat |
記述例
# 1分ごと
* * * * * root touch /home/ubuntu/test.txt
# 1:00〜1:59まで1分ずつ実行
* 1 * * * root touch /home/ubuntu/test.txt
# 毎日1:00に実行
0 1 * * * root touch /home/ubuntu/test.txt
# 毎月12日~20日00:00に実行
0 0 12-20 * * root touch /home/ubuntu/test.txt
# 毎週月曜日〜金曜日00:00に実行
0 0 * * 1-5 root touch /home/ubuntu/test.txt
オプション
# 設定されているcronを表示する
$ crontab -l
# 全てのcronを削除、-eオプションと押し間違えないように注意
$ crontab -r