displaycron.sh
Der Befehl wird für minütliche Einträge mit hervorgehoben.
#!/bin/bash
# displaycron.sh
# By bed
# $Revision: 1.4 $
# Liest die Crontab aus und stellt sie chronologisch in Stunde:Minute dar
# Einträge, die minütlich aufgerufen werden, also nur "*" haben, werden nur einmal dargestellt,
# um die Anzeige übersichtlicher zu machen
OUT=/path/to/domain/cron.html
RAW=cron.raw
TEMP_CRONTAB=temp_crontab.txt
# Lies die Crontab in eine temporäre Datei
crontab -l > $TEMP_CRONTAB
# Erstelle die HTML-Datei und den Header
cat <<EOF > $OUT
<!DOCTYPE html>
<html>
<head>
</head>
<body>
EOF
# Erstelle die Tabelle und den Tabellenkopf
cat <<EOF >> $OUT
<table border="1">
<thead>
<tr><th>hh:mm</th><th>Minute</th><th>Stunde</th><th>Tag</th><th>Monat</th><th>Wochentag</th><th>Befehl</th></tr>
</thead>
<tbody>
EOF
# Verarbeite die temporäre Crontab-Datei
awk '
BEGIN {
E24 = "0"; for(i=1; i<24; ++i) E24 = E24 "," i
E60 = E24; for(i=24; i<60; ++i) E60 = E60 "," i
}
/^#/ { next }
{
if ($1 == "*") n_minutes = split(E60, minute, ",")
else n_minutes = split($1, minute, ",")
if ($2 == "*") n_hours = split(E24, hour, ",")
else n_hours = split($2, hour, ",")
for (h = 1; h <= n_hours; ++h)
for (m = 1; m <= n_minutes; ++m)
printf("%02d:%02d\t%s\n", hour[h], minute[m], $0)
}
' $TEMP_CRONTAB | sort > $RAW
# Generiere die Tabelleinträge
awk '
BEGIN {
minutely_displayed_cmds[""] = 0
}
{
time_str = $1
# Zusammenbau des Befehls
cmd = $7
for (i = 8; i <= NF; ++i) cmd = cmd " " $i
# Überprüfen, ob der Befehl minütlich ist und bereits angezeigt wurde
if ($2 == "*" && $3 == "*" && $4 == "*" && $5 == "*" && $6 == "*") {
if (!minutely_displayed_cmds[cmd]) {
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><strong>%s</strong></td></tr>\n", time_str, $2, $3, $4, $5, $6, cmd)
minutely_displayed_cmds[cmd] = 1
}
} else {
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", time_str, $2, $3, $4, $5, $6, cmd)
}
}
' $RAW >> $OUT
# Schließe die Tabelle und den HTML-Code ab
cat <<EOF >> $OUT
</tbody></table>
</body>
</html>
EOF
# Bereinige temporäre Dateien
rm $TEMP_CRONTAB $RAW