是否有一个命令或现有的脚本可以让我一次查看*NIX系统的所有调度cron作业?我希望它包括所有用户crontab,以及/etc/crontab,以及/etc/cron.d.中的任何内容如果能在/etc/ crontable中看到run-parts运行的特定命令,那就太好了。

理想情况下,我希望输出以良好的列形式,并以某种有意义的方式排序。

然后,我可以合并来自多个服务器的这些清单,以查看总体的“事件时间表”。

我本来打算自己写一个这样的脚本,但如果有人已经费心了……


当前回答

这个脚本将Crontab输出到一个文件中,并列出所有确认没有Crontab条目的用户:

for user in $(cut -f1 -d: /etc/passwd); do 
  echo $user >> crontab.bak
  echo "" >> crontab.bak
  crontab -u $user -l >> crontab.bak 2>> > crontab.bak
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(如果它包含空格呢?在这样的文件中不太可能,但你永远不会知道)。

我认为一个更好的内衬是下面。例如,如果您在NIS或LDAP中有用户,他们不会在/etc/passwd中这将为您提供每个已登录用户的crontabs。

for I in `lastlog | grep -v Never | cut -f1 -d' '`; do echo $I ; crontab -l -u $I ; 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

对Kyle Burton的回答稍加改进,改进了输出格式:

#!/bin/bash
for user in $(cut -f1 -d: /etc/passwd)
do echo $user && crontab -u $user -l
echo " "
done

这取决于您的cron版本。在FreeBSD上使用Vixie cron,我可以做这样的事情:

(cd /var/cron/tabs && grep -vH ^# *) 

如果我想要更多的制表符分隔,我可能会这样做:

(cd /var/cron/tabs && grep -vH ^# * | sed "s/:/      /")

这是sed替换部分中的一个文字制表符。

在/etc/passwd中遍历用户并为每个用户执行crontab -l -u $user可能更独立于系统。