-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcronaudit
More file actions
executable file
·135 lines (118 loc) · 2.94 KB
/
Copy pathcronaudit
File metadata and controls
executable file
·135 lines (118 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
# List all scheduled tasks across the system in one place.
# Covers: user crontabs, /etc/crontab, /etc/cron.d/*, cron.{hourly,daily,weekly,
# monthly}, at jobs, and systemd timers.
# Usage: cronaudit
if [ "$(id -u)" -ne 0 ]; then
echo "Error: Must run as root (use sudo)."
exit 1
fi
divider() {
echo ""
echo "=== $1 ==="
echo ""
}
# Drop comments, blank lines, and environment assignments (NAME=value), which
# cron treats as variable settings rather than scheduled jobs.
strip_noise() {
grep -vE '^[[:space:]]*#' \
| grep -vE '^[[:space:]]*$' \
| grep -vE '^[[:space:]]*[A-Za-z_][A-Za-z0-9_]*[[:space:]]*='
}
# --- User crontabs ---
divider "USER CRONTABS"
found_user_cron=false
while IFS=: read -r user _; do
crontab=$(crontab -l -u "$user" 2>/dev/null | strip_noise)
if [ -n "$crontab" ]; then
found_user_cron=true
echo "[$user]"
printf '%s\n' "$crontab" | while IFS= read -r line; do
echo " $line"
done
echo ""
fi
done < /etc/passwd
if [ "$found_user_cron" = false ]; then
echo " (none)"
fi
# --- /etc/crontab ---
divider "SYSTEM CRONTAB (/etc/crontab)"
if [ -f /etc/crontab ]; then
content=$(strip_noise < /etc/crontab)
if [ -n "$content" ]; then
printf '%s\n' "$content" | while IFS= read -r line; do
echo " $line"
done
else
echo " (no active entries)"
fi
else
echo " (not found)"
fi
# --- /etc/cron.d/ ---
divider "CRON DROP-INS (/etc/cron.d/)"
if [ -d /etc/cron.d ]; then
found_cron_d=false
for file in /etc/cron.d/*; do
[ -f "$file" ] || continue
content=$(strip_noise < "$file")
if [ -n "$content" ]; then
found_cron_d=true
echo "[$(basename "$file")]"
printf '%s\n' "$content" | while IFS= read -r line; do
echo " $line"
done
echo ""
fi
done
if [ "$found_cron_d" = false ]; then
echo " (empty)"
fi
else
echo " (directory not found)"
fi
# --- Periodic scripts ---
divider "PERIODIC SCRIPTS"
for period in hourly daily weekly monthly; do
dir="/etc/cron.${period}"
[ -d "$dir" ] || continue
found_period=false
for script in "$dir"/*; do
[ -e "$script" ] || continue
name=$(basename "$script")
[ "$name" = ".placeholder" ] && continue
if [ "$found_period" = false ]; then
echo "[$period]"
found_period=true
fi
echo " $name"
done
[ "$found_period" = true ] && echo ""
done
# --- at jobs ---
divider "AT JOBS"
if command -v atq > /dev/null 2>&1; then
at_jobs=$(atq 2>/dev/null)
if [ -n "$at_jobs" ]; then
printf '%s\n' "$at_jobs" | while IFS= read -r line; do
echo " $line"
done
else
echo " (none)"
fi
else
echo " (at not installed)"
fi
# --- systemd timers ---
divider "SYSTEMD TIMERS"
if command -v systemctl > /dev/null 2>&1; then
timers=$(systemctl list-timers --all --no-pager 2>/dev/null)
if [ -n "$timers" ]; then
printf '%s\n' "$timers"
else
echo " (none)"
fi
else
echo " (systemd not available)"
fi