この記事には広告を含む場合があります。
記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。
目次
cron の動作ログの確認
CentOS や Ubuntu などの Linux 環境では、定期処理を行いたいときは crontab に記載して動作させます。
動けば便利な cron 処理ですが、設定したはずの定期実行が動作していないときには冷や汗ものです。そこで今回は cron のログの確認方法や動作確認を行う方法をご紹介します。
cron の動作ログの確認する
[CentOS]cron の動作ログの場所
CentOS にて定期実行が行われたときに記述されるログのパスは
/var/log/cron
になります。
ログのサンプル
[root@example log]# cat cron Feb 28 19:32:01 hostname run-parts(/etc/cron.daily)[19073]: finished logrotate Feb 28 19:32:01 hostname run-parts(/etc/cron.daily)[19061]: starting man-db.cron Feb 28 19:32:01 hostname run-parts(/etc/cron.daily)[19084]: finished man-db.cron
[Ubuntu]cron の動作ログの場所
Ubuntu には、CentOS のような定期実行のみが記述される専用のログが無いため、定期実行が行われたことを確認するときは
/var/log/syslog
とシステムログを確認します。
ログのサンプル
root@hostname:/var/log# cat syslog Feb 28 19:25:01 hostname CRON[6548]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )) (省略)
cron のサービスが動作しているかを確認する
cron が動作しないとき、まずは cron のサービスが正常に動作しているか確認する必要があります。CentOS や Ubuntu など主要な Linux では共通で「systemctl」コマンドがあります。
[CentOS]cron が動作しているかを確認するコマンド
CentOS で cron の定期実行が正常に動作しているか確認したときは、
systemctl status crond
のコマンドを実行します。
[CentOS]サービスの確認サンプル
[centos@hostname]$ systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-01-31 19:07:47 JST; 1 months 1 days ago
Main PID: 3901 (crond)
CGroup: /system.slice/crond.service
└─3901 /usr/sbin/crond -n
Jan 31 19:07:47 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal systemd[1]: Started Command Scheduler.
Jan 31 19:07:48 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal crond[3901]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 92% if used.)
Jan 31 19:07:48 ip-xxx-xxx-xxx-xxx.ap-northeast-1.compute.internal crond[3901]: (CRON) INFO (running with inotify support)
[Ubuntu]cron が動作しているかを確認するコマンド
Ubuntu で定期実行を確認するときのコマンドは
systemctl status cron
です。
[Ubuntu]サービスの確認サンプル
root@hostname:# systemctl status cron
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2018-11-10 10:46:56 JST; 2 months 25 days ago
Docs: man:cron(8)
Main PID: 882 (cron)
Tasks: 1 (limit: 1152)
CGroup: /system.slice/cron.service
└─882 /usr/sbin/cron -f
Feb 28 19:17:01 hostname CRON[8461]: pam_unix(cron:session): session closed for user root

