LoginSignup
195
205

More than 3 years have passed since last update.

Cronの使い方とテクニックと詰まったところ

Last updated at Posted at 2018-06-13

Cronとは

毎日、この時間にプログラムを実行したい!
一時間に一回、検査を走らせたい!
毎月一度、バックアップしたい!

という感情になったことは、誰しもあるでしょう。

そんなとき、Cronの出番です

Cronは起動している限り定期的にプログラムを実行してくれるアプリケーションです。
(WindowsではタスクスケジューラというアプリケーションがGUIで提供されています)

はじめに

今回は、Ubuntu 16.04でやってみたいと思います。

使い方

1. Cronの設定ファイルを確認する

tree
/etc/
├── cron.d
│   ├── anacron
├── cron.daily
├── cron.hourly
├── cron.monthly
├── crontab
├── cron.weekly

cron.d

このフォルダに入った拡張子のないファイルが実行されます。
普段はこちらのフォルダ内にファイルを作成して操作を行います

cron.***

こちらは、日ごと、時間ごと、月ごと、など、
実行しやすいように既に作成されています。この中に実行ファイルを入れれば実行できますが、
ファイルが膨大にならない限りcron.dに個人で作成したほうが楽かもしれません

crontab

このファイルはcronの一番メインのファイルです。
こちらに記載しても動きますが、このファイルの形式を元に動かすことになるので、
編集する際はしっかりバックアップをとっておくことをおすすめします。

2. cron.dにファイルを作成する

ここでは、拡張子のない任意の名前のファイル = test_cron とします

bash
$ cp /etc/crontab /etc/cron.d/test_cron

3. cronの説明

/etc/cron.d/test_cronを編集します

/etc/cron.d/test_cron

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

コメントアウトされている通りに記載します。

test_cron
# m h dom mon dow user  command
分 時 日 月 曜日 ユーザー コマンド

# 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

4. Cronの実行

$ sudo service cron restart

テクニック

Cronのログを出す

$ vim /etc/rsyslog.d/50-default.conf

# コメントアウトを外す
# cron.*                          /var/log/cron.log

$ sudo service rsyslog restart

Cronで実行しているコマンドのログを出す

test_cron
* *    * * *  root   touch /home/ubuntu/test.txt >> /[通常ログファイルの場所] 2>> [エラーログファイルの場所]

数秒毎に実行する

test_cron
# for i in `seq [何秒から] [何秒ごと] [何秒まで]`;do (sleep ${i}; [実行するコマンド] ) & done;
# 10秒ごと
* *    * * *  root   for i in `seq 0 10 59`;do (sleep ${i}; touch /home/ubuntu/test.txt ) & done;

外部ファイルから環境変数を読み込み、何秒ごとに実行するかを指定する

/home/ubuntu/.env
TIME=15
test_cron
# bashになっていることを確認する
SHELL=/bin/bash
* *    * * *  root   for i in `source /home/ubuntu/.env && seq 0 ${TIME} 59`;do (sleep ${i}; touch /home/ubuntu/test.txt ) & done;

詰まったところ

1. Tabキー等空白文字の文法ミス

Tab,Spaceキーの場所

*[Space]*[Tab]*[Space]*[Space]*[Tab]root[Tab]Commands   

2. 起動コマンドで起動していない

$ sudo service cron status

Active: active (running)

になってることを確認する

3. Sourceコマンドを使用する際、Bashになっていない

test_cron
# bashになっていることを確認する
SHELL=/bin/bash

以上

2019/08/01 : まだまだ現役動作してます

195
205
0

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
  3. You can use dark theme
What you can do with signing up
195
205