cronの名前は、ギリシャ語のChronosに由来します。Chronosは時間に使用されます。これは、Linuxシステムが指定された時間間隔でコマンドまたはスクリプトの実行を自動化するためのデーモンです。
このチュートリアルでは、Linuxシステムのユーザーに対してスケジュールされているすべてのcronジョブを一覧表示するためのいくつかのオプションを示します。
現在のユーザーのcronジョブを一覧表示する方法
デフォルトのcrontabコマンドは、現在ログインしているユーザーに対して機能します。現在のユーザーに対してスケジュールされているすべてのcronジョブを一覧表示し、次を実行できます。
crontab –l
出力:
すべてのユーザーcronジョブは、通常、 / var / spool / cron / crontabsの下にあります。 ディレクトリ。その名前のすべてのユーザーアカウントに対して、個別のファイルが作成されます。
他のユーザーのcronジョブを一覧表示する
rootまたはsudoの特権を持つユーザーは、他のユーザーのスケジュールされたcronジョブを表示することもできます。 -uの後にユーザー名を使用して、特定のユーザーに属するすべてのジョブを一覧表示します。
例:
sudo crontab –u username –l
usernameを、cronジョブを表示する実際のユーザー名に置き換えます。
システムごとに実行されているcronジョブの一覧表示
rootユーザーは、オペレーティングシステムのcrontabにアクセスして変更できます。次のコマンドをrootまたはsudo特権アカウントとして実行すると、システムのcronジョブを表示できます。
less /etc/crontab
出力:
# /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 # 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 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 )
毎時のcronジョブを一覧表示する方法
/ettc/cron.hourlyディレクトリを表示して、1時間ごとに実行するようにスケジュールされているすべてのcronジョブを見つけることができます。
ls -la /etc/cron.hourly
出力:
total 20 drwxr-xr-x 2 root root 4096 Apr 23 2020 . drwxr-xr-x 142 root root 12288 Jan 19 15:21 .. -rw-r--r-- 1 root root 102 Feb 14 2020 .placeholder
上記の出力は、1時間ごとに実行するcronジョブスケジュールがないことを示しています。パッケージマネージャーによるディレクトリの誤った削除を回避するために作成されたファイル.placeholderが各ディレクトリに表示されます。ディレクトリに他のファイルが存在しない場合。
毎日のcronジョブを一覧表示する方法
同様に、毎日実行するようにスケジュールされたすべてのジョブを一覧表示できます。ほとんどのアプリケーションジョブはこのディレクトリにあります。
ls -la /etc/cron.daily
出力:
total 72 drwxr-xr-x 2 root root 4096 Dec 28 15:28 . drwxr-xr-x 142 root root 12288 Jan 19 15:21 .. -rwxr-xr-x 1 root root 311 Jul 16 2019 0anacron -rwxr-xr-x 1 root root 539 Apr 13 2020 apache2 -rwxr-xr-x 1 root root 376 Dec 5 2019 apport -rwxr-xr-x 1 root root 1478 Apr 9 2020 apt-compat -rwxr-xr-x 1 root root 355 Dec 29 2017 bsdmainutils -rwxr-xr-x 1 root root 384 Nov 19 2019 cracklib-runtime -rwxr-xr-x 1 root root 1187 Sep 6 2019 dpkg -rwxr-xr-x 1 root root 377 Jan 21 2019 logrotate -rwxr-xr-x 1 root root 1123 Feb 25 2020 man-db -rw-r--r-- 1 root root 102 Feb 14 2020 .placeholder -rwxr-xr-x 1 root root 4574 Jul 18 2019 popularity-contest -rwxr-xr-x 1 root root 383 Jan 6 2020 samba -rwxr-xr-x 1 root root 214 Apr 2 2020 update-notifier-common
毎週のcronジョブを一覧表示する方法
毎週のcronジョブは/etc/cron.weeklyディレクトリでスケジュールされます。
ls -la /etc/cron.weekly
出力:
total 32 drwxr-xr-x 2 root root 4096 Apr 23 2020 . drwxr-xr-x 142 root root 12288 Jan 19 15:21 .. -rwxr-xr-x 1 root root 312 Jul 16 2019 0anacron -rwxr-xr-x 1 root root 813 Feb 25 2020 man-db -rw-r--r-- 1 root root 102 Feb 14 2020 .placeholder -rwxr-xr-x 1 root root 211 Apr 2 2020 update-notifier-common
MontlyCronジョブを一覧表示する方法
すべての月次cronジョブは、/ etc/cron.monthlyディレクトリでスケジュールされます。
ls -la /etc/cron.monthly
出力:
total 24 drwxr-xr-x 2 root root 4096 Apr 23 2020 . drwxr-xr-x 142 root root 12288 Jan 19 15:21 .. -rwxr-xr-x 1 root root 313 Jul 16 2019 0anacron -rw-r--r-- 1 root root 102 Feb 14 2020 .placeholder
アプリケーション固有のcronジョブを表示する方法
アプリケーションの5月は、通常の作業のためにcronジョブをスケジュールしました。これらのジョブは、1時間ごと、1日ごと、1週間ごと、または1か月ごとのcronジョブの下にあります。
たとえば、ApacheWebサーバーは/etc/cron.dailyの下にcronジョブファイルを作成しました。これは、ジョブが毎日実行されることを意味します。以下のようにファイルコンテンツにアクセスすると、cronジョブのコンテンツを確認できます。
cat /etc/cron.daily/apache2
出力:
#!/bin/sh # run htcacheclean if set to 'cron' mode set -e set -u type htcacheclean > /dev/null 2>&1 || exit 0 [ -e /etc/default/apache-htcacheclean ] || exit 0 # edit /etc/default/apache-htcacheclean to change this HTCACHECLEAN_MODE=daemon HTCACHECLEAN_RUN=auto HTCACHECLEAN_SIZE=300M HTCACHECLEAN_PATH=/var/cache/apache2/mod_cache_disk HTCACHECLEAN_OPTIONS="" . /etc/default/apache-htcacheclean [ "$HTCACHECLEAN_MODE" = "cron" ] || exit 0 htcacheclean ${HTCACHECLEAN_OPTIONS} \ -p${HTCACHECLEAN_PATH} \ -l${HTCACHECLEAN_SIZE}
結論
このチュートリアルでは、Linuxシステムでcronジョブを表示、一覧表示、または表示する方法を学びました。さらに、時間単位、日単位、週単位、または月単位で実行されるcronジョブの詳細が見つかりました。