LoginSignup
1

More than 1 year has passed since last update.

posted at

updated at

crontabでコマンドを定期実行する

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

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1