如何在Linux中列出Cron作业

时间:2020-03-05 15:27:23  来源:igfitidea点击:

对于Linux来自动执行我们将必须执行自己的任务,例如运行脚本和执行特定命令,我们使用一个名为Cron的工具。
在Linux系统上,Cron实用程序是以特定时间间隔自动执行脚本运行的首选方法。

在本文中,我们将介绍如何在Crontab列表中查看/列出计划的作业。

与大多数克兰斯(例如,vixie-cron debian/ubuntu默认值,cronie fedora默认值,solaris cron ...)我们可以通过以下方式获取当前用户的计划Cron作业列表:

$crontab -l

或者对于另一个用户通过

# crontab -u username -l

或者,我们可以查找假脱机文件。
通常,它们保存在/var/spool/cron下,例如,对于VCRON,以下目录/var/spool/cron/crontabs包含除root用户身体外的所有用户的所有已配置CRONTABS,它们也能够通过System-宽CRONTAB配置作业,该机构位于:

/etc/crontab

要查看它,请执行以下命令:

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.d style config目录,用于系统cron作业:

/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 Jan 19 17:12 ..
-rw-r--r--  1 root root  102 Nov  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 Jan 19 17:12 ..
-rw-r--r--  1 root root   102 Nov  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 Nov 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 Nov 17  2014 passwd
-rwxr-xr-x  1 root root  2417 Jan 13  2013 popularity-contest
-rwxr-xr-x  1 root root   214 Mar 27  2016 update-notifier-common
-rwxr-xr-x  1 root root   328 May 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 Jan 19 17:12 ..
-rw-r--r--  1 root root  102 Nov  9  2013 .placeholder
-rwxr-xr-x  1 root root  730 Nov 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  2016 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 Jan 19 17:12 ..
-rw-r--r--  1 root root  102 Nov  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 Nov 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 Nov 17  2014 passwd
-rwxr-xr-x 1 root root  2417 Jan 13  2013 popularity-contest
-rwxr-xr-x 1 root root   214 Mar 27  2016 update-notifier-common
-rwxr-xr-x 1 root root   328 May 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

最重要的是,我们可以"在"作业(/var/spool//),anacron(/etc/anacrontab和/var/spool/anacron/)

通过系统CRONTAB或者Via Anacron中的运行部件条目管理正确和及时执行这些脚本。

使用SystemD(在Fedora,CentOS 7等)周期性工作执行,可以通过定时器单元配置。
启用的系统定时器可以通过:

$systemctl list-timers