如何在 Linux 中列出 Cron 作業

為了讓 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 包含所有用戶的所有配置的 crontabs – 除了 root 用戶,他也可以通過以下方式配置作業系統範圍的 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

查看軟件特定的 Cronjobs

我們可以使用 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 中的運行部分條目或通過 anacron 進行管理的。

使用 systemd(在 Fedora、CentOS 7 等)定期作業執行還可以通過計時器單元進行配置。 啟用的系統計時器可以通過以下方式顯示:

$ systemctl list-timers

我們已經看到了您可以在大多數 Linux 系統上找到 cron 作業的所有默認目錄。 如果您有任何問題或意見,請在下面的評論部分發表。

另請閱讀:

  • 如何在 Ubuntu 18.04 上檢查 Cron 日誌
  • 如何使用 Cron、Anacron 和 at 命令在 Linux 上調度作業
  • 如何使用 Linux Crontab 文件設置 Cron 作業