如何列出所有用户的所有cron作业?
是否有命令或者现有脚本可以让我立即查看* NIX系统的所有计划cron作业?我希望它包括所有用户crontabs和/ etc / crontab,以及/ etc / cron.d中的所有内容。看到由/ etc / crontab中的run-parts运行的特定命令也将很不错。
理想情况下,我希望输出以漂亮的列形式并以某种有意义的方式排序。
然后,我可以合并来自多个服务器的这些列表,以查看整个"事件时间表"。
我本来要写这样的脚本,但是如果有人已经麻烦了...
解决方案
我们将必须以root用户身份运行,但是:
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
将遍历列出其crontab的每个用户名。 crontabs由各自的用户拥有,因此我们将无法看到其他用户的crontab,而无论他们是他们还是root。
编辑
如果我们想知道crontab属于哪个用户,请使用echo $ user。
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; 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可能是与系统更不相关的。
我最终写了一个脚本(我试图教自己bash脚本的精髓,所以这就是为什么我们在这里看不到Perl之类的原因)。这并不是一件简单的事情,但是它满足了我的大部分需求。它使用Kyle的建议来查找单个用户的crontab,但也处理/ etc / crontab(包括/etc/cron.hourly、/etc/cron.daily中的run-parts启动的脚本)。等)以及/ etc / cron.d目录中的作业。它需要所有这些内容,并将它们合并到如下所示的显示中:
mi h d m w user command 09,39 * * * * root [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -r -0 rm 47 */8 * * * root rsync -axE --delete --ignore-errors / /mirror/ >/dev/null 17 1 * * * root /etc/cron.daily/apt 17 1 * * * root /etc/cron.daily/aptitude 17 1 * * * root /etc/cron.daily/find 17 1 * * * root /etc/cron.daily/logrotate 17 1 * * * root /etc/cron.daily/man-db 17 1 * * * root /etc/cron.daily/ntp 17 1 * * * root /etc/cron.daily/standard 17 1 * * * root /etc/cron.daily/sysklogd 27 2 * * 7 root /etc/cron.weekly/man-db 27 2 * * 7 root /etc/cron.weekly/sysklogd 13 3 * * * archiver /usr/local/bin/offsite-backup 2>&1 32 3 1 * * root /etc/cron.monthly/standard 36 4 * * * yukon /home/yukon/bin/do-daily-stuff 5 5 * * * archiver /usr/local/bin/update-logs >/dev/null
请注意,它显示了用户,并且按小时和分钟显示或者多或者少的排序,以便我可以看到每日时间表。
到目前为止,我已经在Ubuntu,Debian和Red Hat AS上对其进行了测试。
#!/bin/bash
# System-wide crontab file and cron job directory. Change these for your system.
CRONTAB='/etc/crontab'
CRONDIR='/etc/cron.d'
# Single tab character. Annoyingly necessary.
tab=$(echo -en "\t")
# Given a stream of crontab lines, exclude non-cron job lines, replace
# whitespace characters with a single space, and remove any spaces from the
# beginning of each line.
function clean_cron_lines() {
while read line ; do
echo "${line}" |
egrep --invert-match '^($|\s*#|\s*[[:alnum:]_]+=)' |
sed --regexp-extended "s/\s+/ /g" |
sed --regexp-extended "s/^ //"
done;
}
# Given a stream of cleaned crontab lines, echo any that don't include the
# run-parts command, and for those that do, show each job file in the run-parts
# directory as if it were scheduled explicitly.
function lookup_run_parts() {
while read line ; do
match=$(echo "${line}" | egrep -o 'run-parts (-{1,2}\S+ )*\S+')
if [[ -z "${match}" ]] ; then
echo "${line}"
else
cron_fields=$(echo "${line}" | cut -f1-6 -d' ')
cron_job_dir=$(echo "${match}" | awk '{print $NF}')
if [[ -d "${cron_job_dir}" ]] ; then
for cron_job_file in "${cron_job_dir}"/* ; do # */ <not a comment>
[[ -f "${cron_job_file}" ]] && echo "${cron_fields} ${cron_job_file}"
done
fi
fi
done;
}
# Temporary file for crontab lines.
temp=$(mktemp) || exit 1
# Add all of the jobs from the system-wide crontab file.
cat "${CRONTAB}" | clean_cron_lines | lookup_run_parts >"${temp}"
# Add all of the jobs from the system-wide cron directory.
cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}" # */ <not a comment>
# Add each user's crontab (if it exists). Insert the user's name between the
# five time fields and the command.
while read user ; do
crontab -l -u "${user}" 2>/dev/null |
clean_cron_lines |
sed --regexp-extended "s/^((\S+ +){5})(.+)$/${user} /" >>"${temp}"
done < <(cut --fields=1 --delimiter=: /etc/passwd)
# Output the collected crontab lines. Replace the single spaces between the
# fields with tab characters, sort the lines by hour and minute, insert the
# header line, and format the results as a table.
cat "${temp}" |
sed --regexp-extended "s/^(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(.*)$/\t\t\t\t\t\t/" |
sort --numeric-sort --field-separator="${tab}" --key=2,1 |
sed "1i\mi\th\td\tm\tw\tuser\tcommand" |
column -s"${tab}" -t
rm --force "${temp}"
在Ubuntu或者debian下,我们可以通过/ var / spool / cron / crontabs /查看crontab,然后在其中找到每个用户的文件。当然,这仅适用于特定于用户的crontab。
感谢这个非常有用的脚本。在旧系统(Red Hat Enterprise 3,它处理不同的egrep和字符串中的制表符)以及在/etc/cron.d/中没有任何内容的其他系统上运行该脚本时,我遇到了一些小问题。因此,这里有一个补丁程序可以使其在这种情况下工作:
2a3,4
> #See: http://stackoverflow.com/questions/134906/how-do-i-list-all-cron-jobs-for-all-users
>
27c29,30
< match=$(echo "${line}" | egrep -o 'run-parts (-{1,2}\S+ )*\S+')
---
> #match=$(echo "${line}" | egrep -o 'run-parts (-{1,2}\S+ )*\S+')
> match=$(echo "${line}" | egrep -o 'run-parts.*')
51c54,57
< cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}" # */ <not a comment>
---
> sys_cron_num=$(ls /etc/cron.d | wc -l | awk '{print }')
> if [ "$sys_cron_num" != 0 ]; then
> cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}" # */ <not a comment>
> fi
67c73
< sed "1i\mi\th\td\tm\tw\tuser\tcommand" |
---
> sed "1i\mi${tab}h${tab}d${tab}m${tab}w${tab}user${tab}command" |
我不确定第一个egrep中的更改是否是一个好主意,但是好了,此脚本已经在RHEL3、4、5和Debian5上进行了测试,没有任何问题。希望这可以帮助!
我喜欢上面简单的一线回答:
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
但是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)。
getent passwd | cut -d: -f1 | perl -e'while(<>){chomp;$l = `crontab -u $_ -l 2>/dev/null`;print "$_\n$l\n" if $l}'
这样可以避免直接与passwd混淆,跳过没有cron条目的用户,对于有cron条目的用户,它会打印出用户名以及crontab。
不过,通常将其放到这里,这样我以后可以找到它,以防万一我需要再次搜索它。
通过改进输出格式对Kyle Burton的答案进行了一些细微的改进:
#!/bin/bash for user in $(cut -f1 -d: /etc/passwd) do echo $user && crontab -u $user -l echo " " done

