Listar trabajos en cron de todos los usuarios

Esta mañana he necesitado listar todos los trabajos de cron para todos los usuarios. Se me ha ocurrido utilizar:

n@test:~$ sudo crontab -l
00 06 * * * /usr/sbin/***
00 20 * * * /usr/sbin/***

Pero esto sólo muestra los trabajos generados manualmente para root y no los automáticos o del resto de usuarios.

Tras un par de búsquedas en Google he encontrado la respuesta perfecta en, dónde sino, Stack Overflow.

 

Cómo se trata de un script en bash, no había github de por medio y funciona perfectamente ahí va.

#!/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}"

Como la mayoría de scripts en bash, se copia el contenido a un .sh, se le dan permisos de ejecución y se lanza.

Ejemplo:

n@test:~$ wget https://gist.githubusercontent.com/n4txo/e36d135003f7f515ce55/raw/55f9e4c35560864b4ca2b442fdec591a99c70f58/listcronjobs.sh
--2014-11-08 12:48:54--  https://gist.githubusercontent.com/n4txo/e36d135003f7f515ce55/raw/55f9e4c35560864b4ca2b442fdec591a99c70f58/listcronjobs.sh
Resolviendo gist.githubusercontent.com (gist.githubusercontent.com)... 192.30.252.159
Conectando con gist.githubusercontent.com (gist.githubusercontent.com)[192.30.252.159]:443... conectado.
Petición HTTP enviada, esperando respuesta... 200 OK
Longitud: no especificado 
Grabando a: “listcronjobs.sh”

    [ <=>    ] 2.582       --.-K/s   en 0s      

2014-11-08 12:48:56 (12,4 MB/s) - “listcronjobs.sh” guardado [2582]
n@test:~$ ls -l
total 4
-rw-rw-r-- 1 n n   2583 nov  8 12:49 listcronjobs.sh
n@test:~$ chmod +x listcronjobs.sh 
n@test:~$ ls -l
total 4
-rwxrwxr-x 1 n n 2583 nov  8 12:49 listcronjobs.sh
n@test:~$ bash listcronjobs.sh
mi  h  d  m  w  user  command
15  5  *  *  *  root  /etc/cron.daily/***
25  5  *  *  *  root  /etc/cron.daily/***
35  5  *  *  *  root  /etc/cron.daily/***
45  5  *  *  *  root  /etc/cron.daily/***

Fácil, seguro y para toda la familia.

Enjoy!

 

·n·

Deja un comentario