Gibt es einen Befehl oder ein bestehendes Skript, mit dem ich alle geplanten Cron-Jobs eines *NIX-Systems auf einmal sehen kann? Ich möchte, dass es alle Benutzer-Crontabs einschließt, sowie /etc/crontab
und was auch immer in /etc/cron.d
steht. Es wäre auch schön, die spezifischen Befehle zu sehen, die von run-parts
in /etc/crontab
ausgeführt werden.
Idealerweise hätte ich die Ausgabe gerne in einer netten Spaltenform und auf eine sinnvolle Weise geordnet.
Ich könnte dann diese Auflistungen von mehreren Servern zusammenführen, um den gesamten "Zeitplan der Ereignisse" zu sehen;
Ich war dabei, ein solches Skript selbst zu schreiben, aber wenn sich schon jemand die Mühe gemacht hat...
Sie müssen dies als root ausführen, aber:
for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
wird in einer Schleife die Crontabs der einzelnen Benutzer auflisten. Die Crontabs gehören den jeweiligen Benutzern, so dass Sie nicht in der Lage sind, die Crontab eines anderen Benutzers zu sehen, ohne dieser oder root zu sein.
Edit
wenn Sie wissen wollen, welchem Benutzer eine Crontab gehört, verwenden Sie echo $user
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
Am Ende habe ich ein Skript geschrieben (ich versuche, mir die Feinheiten der Bash-Skripterstellung beizubringen, deshalb sehen Sie hier nicht so etwas wie Perl). Es ist nicht gerade eine einfache Angelegenheit, aber es tut das meiste, was ich brauche. Es benutzt Kyles Vorschlag, um die Crontabs der einzelnen Benutzer zu durchsuchen, kümmert sich aber auch um /etc/crontab
(einschließlich der Skripte, die von run-parts
in /etc/cron.hourly
, /etc/cron.daily
usw. gestartet werden) und die Jobs im Verzeichnis /etc/cron.d
. Es nimmt all diese und fügt sie zu einer Anzeige wie der folgenden zusammen:
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
Beachten Sie, dass der Benutzer angezeigt wird und mehr oder weniger nach Stunden und Minuten sortiert ist, so dass ich den täglichen Zeitplan sehen kann.
Bislang habe ich es auf Ubuntu, Debian und Red Hat AS getestet.
#!/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}"
Das hängt von der Version von cron ab. Wenn ich Vixie cron unter FreeBSD verwende, kann ich etwa so vorgehen:
(cd /var/cron/tabs && grep -vH ^# *)
wenn ich mehr Tabs haben möchte, könnte ich etwas wie das hier machen:
(cd /var/cron/tabs && grep -vH ^# * | sed "s/:/ /")
Wobei das ein buchstäblicher Tabulator im sed-Ersetzungsteil ist.
Es kann systemunabhängiger sein, eine Schleife durch die Benutzer in /etc/passwd
zu ziehen und crontab -l -u $user
für jeden von ihnen auszuführen.