是否有一个命令或现有的脚本可以让我一次查看*NIX系统的所有调度cron作业?我希望它包括所有用户crontab,以及/etc/crontab,以及/etc/cron.d.中的任何内容如果能在/etc/ crontable中看到run-parts运行的特定命令,那就太好了。
理想情况下,我希望输出以良好的列形式,并以某种有意义的方式排序。
然后,我可以合并来自多个服务器的这些清单,以查看总体的“事件时间表”。
我本来打算自己写一个这样的脚本,但如果有人已经费心了……
是否有一个命令或现有的脚本可以让我一次查看*NIX系统的所有调度cron作业?我希望它包括所有用户crontab,以及/etc/crontab,以及/etc/cron.d.中的任何内容如果能在/etc/ crontable中看到run-parts运行的特定命令,那就太好了。
理想情况下,我希望输出以良好的列形式,并以某种有意义的方式排序。
然后,我可以合并来自多个服务器的这些清单,以查看总体的“事件时间表”。
我本来打算自己写一个这样的脚本,但如果有人已经费心了……
当前回答
虽然许多答案都产生了有用的结果,但我认为为这个任务维护一个复杂的脚本是不值得的。这主要是因为大多数发行版使用不同的cron守护进程。
孩子们和老人们,注意学习。
$ \cat ~jaroslav/bin/ls-crons
#!/bin/bash
getent passwd | awk -F: '{ print $1 }' | xargs -I% sh -c 'crontab -l -u % | sed "/^$/d; /^#/d; s/^/% /"' 2>/dev/null
echo
cat /etc/crontab /etc/anacrontab 2>/dev/null | sed '/^$/d; /^#/d;'
echo
run-parts --list /etc/cron.hourly;
run-parts --list /etc/cron.daily;
run-parts --list /etc/cron.weekly;
run-parts --list /etc/cron.monthly;
像这样跑
$ sudo ls-cron
样本输出(Gentoo)
$ sudo ~jaroslav/bin/ls-crons
jaroslav */5 * * * * mv ~/java_error_in_PHPSTORM* ~/tmp 2>/dev/null
jaroslav 5 */24 * * * ~/bin/Find-home-files
jaroslav * 7 * * * cp /T/fortrabbit/ssh-config/fapps.tsv /home/jaroslav/reference/fortrabbit/fapps
jaroslav */8 1 * * * make -C /T/fortrabbit/ssh-config discover-apps # >/dev/null
jaroslav */7 * * * * getmail -r jazzoslav -r fortrabbit 2>/dev/null
jaroslav */1 * * * * /home/jaroslav/bin/checkmail
jaroslav * 9-18 * * * getmail -r fortrabbit 2>/dev/null
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
RANDOM_DELAY=45
START_HOURS_RANGE=3-22
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
/etc/cron.hourly/0anacron
/etc/cron.daily/logrotate
/etc/cron.daily/man-db
/etc/cron.daily/mlocate
/etc/cron.weekly/mdadm
/etc/cron.weekly/pfl
示例输出(Ubuntu)
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
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 )
/etc/cron.hourly/btrfs-quota-cleanup
/etc/cron.hourly/ntpdate-debian
/etc/cron.daily/apport
/etc/cron.daily/apt-compat
/etc/cron.daily/apt-show-versions
/etc/cron.daily/aptitude
/etc/cron.daily/bsdmainutils
/etc/cron.daily/dpkg
/etc/cron.daily/logrotate
/etc/cron.daily/man-db
/etc/cron.daily/mlocate
/etc/cron.daily/passwd
/etc/cron.daily/popularity-contest
/etc/cron.daily/ubuntu-advantage-tools
/etc/cron.daily/update-notifier-common
/etc/cron.daily/upstart
/etc/cron.weekly/apt-xapian-index
/etc/cron.weekly/man-db
/etc/cron.weekly/update-notifier-common
Pics
Ubuntu:
Gentoo:
其他回答
以@Kyle为基础
for user in $(tail -n +11 /etc/passwd | cut -f1 -d:); do echo $user; crontab -u $user -l; done
为了避免/etc/passwd顶部的注释,
在macosx上
for user in $(dscl . -list /users | cut -f1 -d:); do echo $user; crontab -u $user -l; done
由于这是一个循环通过文件(/etc/passwd)和执行一个动作的问题,我错过了正确的方法,我如何能逐行(和/或逐字段)读取文件(数据流,变量)?:
while IFS=":" read -r user _
do
echo "crontab for user ${user}:"
crontab -u "$user" -l
done < /etc/passwd
它使用:作为字段分隔符逐行读取/etc/passwd。通过read -r user _,我们让$user保存第一个字段和_其余的字段(它只是一个忽略字段的垃圾变量)。
这样,我们就可以使用变量$user调用crontab -u,为了安全起见,我们引用了变量$user(如果它包含空格呢?在这样的文件中不太可能,但你永远不会知道)。
在Solaris上,对于特定的已知用户名:
crontab -l username
要在Solaris上一次性获得所有用户的作业,就像上面的其他文章一样:
for user in $(cut -f1 -d: /etc/passwd); do crontab -l $user 2>/dev/null; done
更新: 请停止建议Solaris上的错误编辑:
你必须以root用户运行,但是:
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
将遍历每个用户名,列出他们的crontab。crontab由各自的用户拥有,因此您将无法看到其他用户的crontab,除非是他们或root用户。
编辑 如果你想知道crontab属于哪个用户,使用echo $user
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
向yukondude表示歉意和感谢。
我已经试着总结了时间设置以便于阅读,尽管这不是一个完美的工作,而且我不会碰“每周五”或“只有周一”的东西。
这是版本10 -现在:
runs much much faster has optional progress characters so you could improve the speed further. uses a divider line to separate header and output. outputs in a compact format when all timing intervals uencountered can be summarised. Accepts Jan...Dec descriptors for months-of-the-year Accepts Mon...Sun descriptors for days-of-the-week tries to handle debian-style dummying-up of anacron when it is missing tries to deal with crontab lines which run a file after pre-testing executability using "[ -x ... ]" tries to deal with crontab lines which run a file after pre-testing executability using "command -v" allows the use of interval spans and lists. supports run-parts usage in user-specific /var/spool crontab files.
我现在在这里发布完整的脚本。
https://gist.github.com/myshkin-uk/d667116d3e2d689f23f18f6cd3c71107