是否有一个命令或现有的脚本可以让我一次查看*NIX系统的所有调度cron作业?我希望它包括所有用户crontab,以及/etc/crontab,以及/etc/cron.d.中的任何内容如果能在/etc/ crontable中看到run-parts运行的特定命令,那就太好了。
理想情况下,我希望输出以良好的列形式,并以某种有意义的方式排序。
然后,我可以合并来自多个服务器的这些清单,以查看总体的“事件时间表”。
我本来打算自己写一个这样的脚本,但如果有人已经费心了……
是否有一个命令或现有的脚本可以让我一次查看*NIX系统的所有调度cron作业?我希望它包括所有用户crontab,以及/etc/crontab,以及/etc/cron.d.中的任何内容如果能在/etc/ crontable中看到run-parts运行的特定命令,那就太好了。
理想情况下,我希望输出以良好的列形式,并以某种有意义的方式排序。
然后,我可以合并来自多个服务器的这些清单,以查看总体的“事件时间表”。
我本来打算自己写一个这样的脚本,但如果有人已经费心了……
当前回答
I ended up writing a script (I'm trying to teach myself the finer points of bash scripting, so that's why you don't see something like Perl here). It's not exactly a simple affair, but it does most of what I need. It uses Kyle's suggestion for looking up individual users' crontabs, but also deals with /etc/crontab (including the scripts launched by run-parts in /etc/cron.hourly, /etc/cron.daily, etc.) and the jobs in the /etc/cron.d directory. It takes all of those and merges them into a display something like the following:
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})(.+)$/\1${user} \3/" >>"${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+) +(.*)$/\1\t\2\t\3\t\4\t\5\t\6\t\7/" |
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}"
其他回答
由于这是一个循环通过文件(/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(如果它包含空格呢?在这样的文件中不太可能,但你永远不会知道)。
这个脚本将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
对Kyle Burton的回答稍加改进,改进了输出格式:
#!/bin/bash
for user in $(cut -f1 -d: /etc/passwd)
do echo $user && crontab -u $user -l
echo " "
done
你必须以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
这个脚本在CentOS中为我列出了环境中的所有cron:
sudo cat /etc/passwd | sed 's/^\([^:]*\):.*$/sudo crontab -u \1 -l 2>\&1/' | grep -v "no crontab for" | sh