linux查看上次crontab时间,如何在 linux 中列出 crontab 作业

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

在本文中,我们将介绍如何查看/列出crontab列表中安排的作业。

对于大多数Crons(例如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 2019 .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 2019 .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中的运行部件条目或通过anacron管理这些脚本的正确和及时执行。

使用systemd(在Fedora,CentOS 7等上),可以通过定时器单元另外配置定期作业执行。 启用的系统计时器可通过以下方式显示:

systemctl list-timers

写在最后

我们已经看到了所有默认目录,您可以在大多数Linux系统上找到cron作业。 如果您有任何问题或意见,请在评论部分发布如下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值