勉強前イメージ
繰り返し、定期的に実行したい際に使用する。
毎分・毎時・毎日決まった時間に実行させる。
基本的にはcronのコト知ってるはずだけど、cronの設定ファイルとは別に確か毎時の設定ファイルとかあって
関係性がよく分かってないから調べたい
調査
そもそもcronとは?
linux系のシステムで使用されるジョブスケジューラーです。
設定したスケジュールに基づいて、一定期間毎にコマンドやスクリプトを自動実行させます。
cronの設定ファイルはどこ?
/etc配下
/etc配下のcronはrootが使用します。
- ディレクトリ構成を見てみた
[root@localhost ~]# tree /etc/cron*
/etc/cron.d
└── 0hourly
/etc/cron.daily
├── logrotate
└── man-db.cron
/etc/cron.deny [error opening dir]
/etc/cron.hourly
└── 0anacron
/etc/cron.monthly
/etc/cron.weekly
/etc/crontab [error opening dir]
-
定期実行処理の場合
- 毎時間、毎日、毎週、毎月実行したい処理を、それぞれ以下のディレクトリに設定ファイルを配置します。
- /etc/cron.daily
- /etc/cron.hourly
- /etc/cron.monthly
- /etc/cron.weekly
- 毎時間、毎日、毎週、毎月実行したい処理を、それぞれ以下のディレクトリに設定ファイルを配置します。
-
定期実行処理とは異なる処理条件の場合
- 毎時間、毎日、毎週、毎月の実行とは異なる条件の場合は以下のファイルに設定を行います。
- /etc/cron.d
- 毎時間、毎日、毎週、毎月の実行とは異なる条件の場合は以下のファイルに設定を行います。
-
システム管理者が設定するもの
- 基本的にはここにはアプリケーションなどのcronは設定しない。
- /etc/crontab
- 基本的にはここにはアプリケーションなどのcronは設定しない。
-
アクセス制御
- ユーザを指定して使用を許可したい場合や、拒否したい場合に記載
- /etc/cron.deny
- /etc/cron.allow
- ユーザを指定して使用を許可したい場合や、拒否したい場合に記載
cron.allow | cron.deny | 対象ユーザ |
---|---|---|
なし | なし | 全ユーザ許可 |
あり | 無視 | cron.allow に記載されているユーザのみ許可 |
なし | あり | cron.deny に記載されていないユーザのみ許可 |
空 | 無視 | なし |
なし | 空 | 全ユーザ許可 |
/var/spool/cron配下
/var/spool/cron配下のcronはユーザが使用者になります。
- ユーザ別のcrontab
- (例)/var/spool/cron/admin[ユーザ名]
- 基本的には変更したいユーザになって、
crontab -e
で編集を行う - /var/spool/cronを直接変更するのは推奨されていない
cronはどうやって動いてるの?
cronはcronデーモンで動いています。
- デーモン確認
ここでは crond
と記述します。
service crond status
==========
Redirecting to /bin/systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 月 2020-11-30 21:31:58 JST; 1 day 23h ago
Main PID: 667 (crond)
CGroup: /system.slice/crond.service
└─667 /usr/sbin/crond -n
11月 30 21:31:58 localhost.localdomain systemd[1]: Started Command Scheduler.
11月 30 21:31:58 localhost.localdomain crond[667]: (CRON) INFO (RANDOM_DELAY will be scaled with ...d.)
11月 30 21:31:58 localhost.localdomain crond[667]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.
==========
cronの設定ファイルの書き方
/etc配下
- manコマンドで確認
/etc配下のcrontabは実行ユーザの設定を行うためのフィールドが存在する
man /etc/crontab
==========
SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition: # .‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ minute (0 ‐ 59)
# | .‐‐‐‐‐‐‐‐‐‐‐‐‐ hour (0 ‐ 23) # | | .‐‐‐‐‐‐‐‐‐‐ day of
month (1 ‐ 31) # | | | .‐‐‐‐‐‐‐ month (1 ‐ 12) OR
jan,feb,mar,apr ... # | | | | .‐‐‐‐ day of week (0 ‐ 6)
(Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | #
* * * * * user‐name command to be executed
==========
- (例)毎日5時にrootユーザがlsを叩くcron設定
分 時 日 月 曜日 ユーザ コマンド
0 5 * * * root ls
/var/spool/cron
ユーザ別のcrontabの設定は/etc配下と違い、ユーザの記載がない
- (例)毎日5時にがlsを叩くcron設定
分 時 日 月 曜日 コマンド
0 5 * * * ls
編集したいときはどうするの?
- rootユーザがadminユーザのcrontabを編集したい場合 ※rootユーザのみ使用できる
crontab -l -u admin
- ログインしているユーザのcrontabを編集したい場合
crontab -e
勉強後イメージ
あまり/etc配下のcron使わないから、そもそも書式違うとか知らなかった...
あとserviceコマンドで動いてるってのも知らなかった。
いつも使ってるからこそしれてよかったです。