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

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

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

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


当前回答

for user in $(cut -f1 -d: /etc/passwd); 
do 
    echo $user; crontab -u $user -l; 
done

其他回答

在Solaris上,对于特定的已知用户名:

crontab -l username

要在Solaris上一次性获得所有用户的作业,就像上面的其他文章一样:

for user in $(cut -f1 -d: /etc/passwd); do crontab -l $user 2>/dev/null; done

更新: 请停止建议Solaris上的错误编辑:

下面剥离了不使用crontab的用户的注释、空行和错误。剩下的就是一个清晰的用户列表和他们的工作。

注意,在第二行中使用了sudo。如果你已经是根用户了,移除它。

for USER in $(cut -f1 -d: /etc/passwd); do \
USERTAB="$(sudo crontab -u "$USER" -l 2>&1)";  \
FILTERED="$(echo "$USERTAB"| grep -vE '^#|^$|no crontab for|cannot use this program')";  \
if ! test -z "$FILTERED"; then  \
echo "# ------ $(tput bold)$USER$(tput sgr0) ------";  \
echo "$FILTERED";  \
echo "";  \
fi;  \
done

示例输出:

# ------ root ------
0 */6 * * * /usr/local/bin/disk-space-notify.sh
45 3 * * * /opt/mysql-backups/mysql-backups.sh
5 7 * * * /usr/local/bin/certbot-auto renew --quiet --no-self-upgrade

# ------ sammy ------
55 * * * * wget -O - -q -t 1 https://www.example.com/cron.php > /dev/null

我在Ubuntu(12到16)和Red Hat(5到7)上使用这个。

我喜欢上面简单的一行字回答:

$(cut -f1 -d: /etc/passwd);执行crontab -u $user -l;完成

但是Solaris没有-u标志,也不会打印正在检查的用户,你可以这样修改:

for user in $(cut -f1 -d: /etc/passwd); do echo User:$user; crontab -l $user 2>&1 | grep -v crontab; done

当一个帐户不允许使用cron等时,您将得到一个用户列表,其中没有crontab抛出的错误。注意,在Solaris中,角色也可以在/etc/passwd中(参见/etc/user_attr)。

这个脚本在CentOS中为我列出了环境中的所有cron:

sudo cat /etc/passwd | sed 's/^\([^:]*\):.*$/sudo crontab -u \1 -l 2>\&1/' | grep -v "no crontab for" | sh

向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