如何列出所有用户的所有cron作业?

问题描述 投票:788回答:23

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

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

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

我本来就要写这样一个剧本,但是如果有人已经麻烦了......

unix cron
23个回答
1076
投票

你必须以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

8
投票

我喜欢上面简单的单行答案:

对于$中的用户(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)。


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

6
投票

从ROOT用户获取列表。

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

5
投票

以下删除了没有crontab的用户的注释,空行和错误。你剩下的就是一份明确的用户及其工作清单。

注意在第二行使用sudo。如果您已经是root用户,请将其删除。

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)上使用它。


4
投票

取决于您的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可能更加系统独立。


3
投票

感谢这个非常有用的脚本。我在旧系统上运行它有一些小问题(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 $1}')
> 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上进行了测试,没有任何问题。希望这可以帮助!


2
投票

建立在@Kyle之上

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

通常在/ etc / passwd顶部避免注释,

在macosx上

for user in $(dscl . -list /users | cut -f1 -d:); do echo $user; crontab -u $user -l; done    

2
投票

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

for I in `lastlog | grep -v Never | cut -f1 -d' '`; do echo $I ; crontab -l -u $I ; done

2
投票

你可以为所有用户列表写:

sudo crontab -u userName -l

,

你也可以去

cd /etc/cron.daily/
ls -l
cat filename

此文件将列出计划

cd /etc/cron.d/
ls -l
cat filename

2
投票

道歉并感谢yukondude。

我试图总结时间设置以便于阅读,虽然这不是一个完美的工作,我不会触及'每个星期五'或'仅在星期一'的东西。

这是版本10 - 它现在:

  • 跑得快得多
  • 具有可选的进度字符,因此您可以进一步提高速度。
  • 使用分隔线来分隔标题和输出。
  • 当总结所有时间间隔时,以紧凑的格式输出。
  • 接受1月份的Jan ... Dec描述符
  • 接受星期一......太阳描述符表示星期几
  • 当失踪时,他试图处理错误的debian风格的dummying-up
  • 尝试使用“[-x ...]”预处理可执行性后处理运行文件的crontab行
  • 尝试使用“command -v”预处理可执行性后处理运行文件的crontab行
  • 允许使用区间跨度和列表。
  • 支持特定于用户的/ var / spool crontab文件中的运行部件使用。

我现在正在这里发布完整的脚本。

https://gist.github.com/myshkin-uk/d667116d3e2d689f23f18f6cd3c71107


301
投票

我最终编写了一个脚本(我正在尝试自己教授bash脚本的细节,所以这就是为什么你在这里看不到类似Perl的东西)。这不是一件简单的事情,但它完成了我所需要的大部分工作。它使用Kyle的建议查找个人用户的crontabs,还处理/etc/crontab(包括run-parts/etc/cron.hourly等中的/etc/cron.daily发布的脚本)和/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})(.+)$/\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}"

1
投票

由于这是循环文件(/etc/passwd)并执行动作的问题,我在How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?上错过了正确的方法:

while IFS=":" read -r user _
do
   echo "crontab for user ${user}:"
   crontab -u "$user" -l
done < /etc/passwd

这使用/etc/passwd作为字段分隔符逐行读取:。通过说read -r user _,我们让$user保持第一个字段和_其余(它只是一个忽略字段的垃圾变量)。

这样,我们就可以使用变量crontab -u来调用$user,我们引用它来确保安全性(如果它包含空格怎么办?在这样的文件中不太可能,但你永远不会知道)。


1
投票

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

crontab -l username

所有其他* Nix将需要-u修饰符:

crontab -u username -l

要在Solaris上同时获取所有用户的作业,就像上面的其他帖子一样:

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

-1
投票

对我来说,看看/ var / spool / cron / crontabs是最好的方法


-2
投票

此脚本将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

174
投票

在Ubuntu或debian下,您可以通过/var/spool/cron/crontabs/查看crontab,然后每个用户的文件都在那里。当然,这仅适用于特定于用户的crontab。

对于Redhat 6/7和Centos,crontab在/var/spool/cron/下。


33
投票

这将显示所有用户的所有crontab条目。

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

28
投票

取决于你的Linux版本,但我使用:

tail -n 1000 /var/spool/cron/*

作为根。非常简单,非常短。

给我输出像:

==> /var/spool/cron/root <==
15 2 * * * /bla

==> /var/spool/cron/my_user <==
*/10 1 * * * /path/to/script

14
投票

Kyle Burton对改进输出格式的一个小改进:

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

13
投票
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。

大部分都放在这里虽然所以我以后可以找到它以防万一我需要再次搜索它。


10
投票

如果使用NIS检查集群,则根据Matt的答案/ var / spool / cron / tabs查看用户是否具有crontab条目的唯一方法。

grep -v "#" -R  /var/spool/cron/tabs

9
投票

这个脚本在CentOS中为我工作,列出了环境中的所有crons:

sudo cat /etc/passwd | sed 's/^\([^:]*\):.*$/sudo crontab -u \1 -l 2>\&1/' | grep -v "no crontab for" | sh
© www.soinside.com 2019 - 2024. All rights reserved.