如何列出所有用户的所有 cron 作业? [英] How do I list all cron jobs for all users?

查看:35
本文介绍了如何列出所有用户的所有 cron 作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有命令或现有脚本可以让我一次查看 *NIX 系统的所有预定 cron 作业?我希望它包含所有用户 crontab,以及 /etc/crontab/etc/cron.d 中的任何内容.在 /etc/crontab 中看到 run-parts 运行的特定命令也很不错.

Is there a command or an existing script that will let me view all of a *NIX system's scheduled cron jobs at once? I'd like it to include all of the user crontabs, as well as /etc/crontab, and whatever's in /etc/cron.d. It would also be nice to see the specific commands run by run-parts in /etc/crontab.

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

Ideally, I'd like the output in a nice column form and ordered in some meaningful way.

然后,我可以合并来自多个服务器的这些列表,以查看整体事件安排".

I could then merge these listings from multiple servers to view the overall "schedule of events."

我正要自己写这样的脚本,但如果有人已经遇到麻烦了......

I was about to write such a script myself, but if someone's already gone to the trouble...

推荐答案

我最终编写了一个脚本(我正在尝试自学 bash 脚本的精髓,所以这就是为什么你在这里看不到 Perl 之类的东西).这不是一件简单的事情,但它完成了我需要的大部分工作.它使用 Kyle 的建议来查找单个用户的 crontabs,但也处理 /etc/crontab(包括 /etc/中由 run-parts 启动的脚本)cron.hourly/etc/cron.daily 等)和 /etc/cron.d 目录中的作业.它需要所有这些并将它们合并为如下所示的显示:

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

请注意,它显示了用户,并且或多或少按小时和分钟排序,以便我可以看到每日时间表.

Note that it shows the user, and more-or-less sorts by hour and minute so that I can see the daily schedule.

到目前为止,我已经在 Ubuntu、Debian 和 Red Hat AS 上对其进行了测试.

So far, I've tested it on Ubuntu, Debian, and 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 "	")

# 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	2	3	4	5	6	7/" |
    sort --numeric-sort --field-separator="${tab}" --key=2,1 |
    sed "1imi	h	d	m	w	user	command" |
    column -s"${tab}" -t

rm --force "${temp}"

这篇关于如何列出所有用户的所有 cron 作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆