Linuxで、スクリプトの実行や特定のコマンドの実行など、他の方法では自分で実行する必要のあるタスクを自動的に実行するには、cronと呼ばれるツールを使用します。 Linuxシステムでは、cronユーティリティは、特定の時間間隔でのスクリプトの実行を自動化するための推奨される方法です。
この記事では、crontabリストでスケジュールされたジョブを表示/一覧表示する方法について説明します。
ほとんどのcron(例:Vixie-Cron-Debian / Ubuntuのデフォルト、Cronie- Fedora デフォルトでは、Solaris Cron …)現在のユーザーのスケジュールされたcronジョブのリストを次の方法で取得します。
$ crontab -l
または別のユーザーの場合
# crontab -u username -l
または、スプールファイルを検索することもできます。 通常、これらは/ var / spool / cronの下に保存されます。たとえば、vcronの場合、次のディレクトリ/ var / spool / cron / crontabsには、すべてのユーザーの構成済みcrontabがすべて含まれています。システム全体のcrontabは、次の場所にあります。
/etc/crontab
表示するには、次のlessコマンドを実行します。
less /etc/crontab
crontabの構造例:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/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
私のコンピューターでは、次のようになります。
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
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 )
cronieを使用(デフォルトはオン) Fedora/ CentOS)、システムcronジョブ用のcron.dスタイルの設定ディレクトリもあります。
/etc/cron.d
いつものように、cron.dディレクトリは、さまざまなパッケージの一部である構成エントリの保守を簡素化します。
便宜上、ほとんどのディストリビューションには、リンク/保存されたスクリプトが定期的に実行されるディレクトリも用意されています。
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
時間単位のcronジョブを一覧表示する方法
時間単位のcronジョブを一覧表示するには、以下のコマンドを実行します
$ ls -la /etc/cron.hourly/
total 12
drwxr-xr-x 2 root root 4096 Apr 24 20:44 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
毎日のcronジョブを一覧表示する方法
次のコマンドを使用して、毎日のcronジョブを一覧表示できます。
$ ls -la /etc/cron.daily/
total 72
drwxr-xr-x 2 root root 4096 Apr 24 20:46 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
-rwxr-xr-x 1 root root 376 Apr 4 2014 apport
-rwxr-xr-x 1 root root 15481 Apr 10 2014 apt
-rwxr-xr-x 1 root root 314 Feb 18 2014 aptitude
-rwxr-xr-x 1 root root 355 Jun 4 2013 bsdmainutils
-rwxr-xr-x 1 root root 256 Mar 7 2014 dpkg
-rwxr-xr-x 1 root root 372 Jan 22 2014 logrotate
-rwxr-xr-x 1 root root 1261 Sep 23 2014 man-db
-rwxr-xr-x 1 root root 435 Jun 20 2013 mlocate
-rwxr-xr-x 1 root root 249 Feb 17 2014 passwd
-rwxr-xr-x 1 root root 2417 May 13 2013 popularity-contest
-rwxr-xr-x 1 root root 214 Mar 27 2017 update-notifier-common
-rwxr-xr-x 1 root root 328 Jul 18 2014 upstart
毎週のcronジョブを一覧表示する方法
毎週のcronジョブを一覧表示します。 以下のコマンドを実行します
$ ls -la /etc/cron.weekly/
total 28
drwxr-xr-x 2 root root 4096 Apr 24 20:46 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
-rwxr-xr-x 1 root root 730 Feb 23 2014 apt-xapian-index
-rwxr-xr-x 1 root root 427 Apr 16 2014 fstrim
-rwxr-xr-x 1 root root 771 Sep 23 2014 man-db
-rwxr-xr-x 1 root root 211 Mar 27 2017 update-notifier-common
毎月のcronジョブを一覧表示する方法
これにより、毎月のcronジョブが一覧表示されます
$ ls -la /etc/cron.monthly/
total 12
drwxr-xr-x 2 root root 4096 Apr 24 20:44 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
ソフトウェア固有のcronジョブを表示する
catコマンドを使用して特定のcronジョブを確認できます。
$ cd /etc/cron.daily/
$ ls -l
total 60
-rwxr-xr-x 1 root root 376 Apr 4 2014 apport
-rwxr-xr-x 1 root root 15481 Apr 10 2014 apt
-rwxr-xr-x 1 root root 314 Feb 18 2014 aptitude
-rwxr-xr-x 1 root root 355 Jun 4 2013 bsdmainutils
-rwxr-xr-x 1 root root 256 Mar 7 2014 dpkg
-rwxr-xr-x 1 root root 372 Jan 22 2014 logrotate
-rwxr-xr-x 1 root root 1261 Sep 23 2014 man-db
-rwxr-xr-x 1 root root 435 Jun 20 2013 mlocate
-rwxr-xr-x 1 root root 249 Feb 17 2014 passwd
-rwxr-xr-x 1 root root 2417 May 13 2013 popularity-contest
-rwxr-xr-x 1 root root 214 Mar 27 2017 update-notifier-common
-rwxr-xr-x 1 root root 328 Jul 18 2014 upstart
$ cat update-notifier-common
#!/bin/sh
set -e
[ -x /usr/lib/update-notifier/package-data-downloader ] || exit 0
# Try to rerun any package data downloads that failed at package install time.
/usr/lib/update-notifier/package-data-downloader
その上、「at」ジョブ(/ var / spool / at / *)、anacron(/ etc / anacrontabおよび/ var / spool / anacron / *)を持つことができます。
これらのスクリプトの正確でタイムリーな実行は、システムcrontabのrun-partsエントリまたはanacronを介して管理されます。
systemdを使用(オン Fedora、CentOS 7など)定期的なジョブの実行は、タイマーユニットを介して追加で構成できます。 有効なシステムタイマーは、次の方法で表示できます。
$ systemctl list-timers
ほとんどのLinuxシステムでcronジョブを見つけることができるすべてのデフォルトディレクトリを見てきました。 ご質問やご意見がございましたら、コメント欄に投稿してください。
また読む:
- Ubuntu18.04でcronログを確認する方法
- Cron、Anacron、およびコマンドを使用してLinuxでジョブをスケジュールする方法
- LinuxCrontabファイルを使用してCronジョブを設定する方法