-
Notifications
You must be signed in to change notification settings - Fork 0
/
crond_install.sh
308 lines (237 loc) · 7.36 KB
/
crond_install.sh
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
##
# Trap on error
# Use 'exit-on-error' (set -e) (EXIT)
set -e
trap 'catch $?' EXIT SIGINT SIGTERM
##
# Function to deal with errors
catch() {
if [ "$1" != "0" ]; then
echo "Installation failed ($1)"
fi
}
echo "Installing /etc/rc.cron"
sudo tee /etc/rc.cron > /dev/null <<'EOF'
#!/bin/bash
##
# Trap on error
set -euo pipefail
trap 'catch $?' EXIT SIGINT SIGTERM
##
# Function to deal with errors
# Start by checking if it exited with error
catch() {
if [ "$1" != "0" ]; then
systemd-cat --priority=emerg --identifier=rc.cron -- echo "rc.cron exited with code $1"
fi
}
find_inode() {
local dir=$1
local inode=$2
local file
for file in $dir/*; do
if [ -f $file ] && [ "$inode" = "`ls -i $file | awk '{print $1}'`" ]; then
find_inode_ret=$(readlink -f $file)
find_inode_num=$(ls -i $find_inode_ret | awk '{print $1}')
return 0
fi
done
find_inode_ret=
find_inode_num=0
return 1
}
find_inode_ret=
find_inode_num=
timer=${1:-"any"}
self_pid=$$
self_name=$(basename $0)
self_path="$(dirname $(realpath "$0"))"
script_path="$self_path/${self_name}.d"
lock_path=/var/lock
if echo $timer | grep -qe '^[0-9]\+:'; then
inode=$(echo $timer | awk -v 'FS=:' '{print $1}')
timer=$(echo $timer | awk -v 'FS=:' '{print $2}')
if ! find_inode $script_path $inode; then
systemd-cat --priority=err --identifier=rc.cron#$inode -- echo "Could not find script with inode $inode"; exit 1
else
script=$find_inode_ret
inode=$find_inode_num
if [ ! -x $script ]; then
systemd-cat --priority=err --identifier=rc.cron#$inode -- echo "The script $script is not executable"; exit 1
fi
fi
if ! (
if ! flock --exclusive -w 180 200; then
systemd-cat --priority=notice --identifier=rc.cron#$inode -- echo "The script $script is already running in a background task, skipping..."; exit 0
fi
echo "Pid: $self_pid, Path: $script" >&200 # Store PID in lock file
systemd-cat --stderr-priority=err --identifier=rc.cron#$inode -- $script $timer
) 200>$lock_path/${self_name}:${inode}.elock
then
systemd-cat --priority=alert --identifier=rc.cron#$inode -- echo "The script $script exited with error $? using timer '$timer'"
fi
exit $?
fi
case $timer in
weekly)
# Not perfekt. For an axample during week 53 and week 1, which both will be bi-weekly
if [ $(($(date +%W) % 2)) -ne 0 ]; then
timer="$timer biweekly"
fi
;;
daily)
dow=$(date +%w)
if [ $dow -gt 0 ] && [ $dow -lt 6 ]; then
timer="$timer weekdays"
else
timer="$timer weekends"
fi
case $dow in
1) timer="$timer mondays" ;;
2) timer="$timer tuesdays" ;;
3) timer="$timer wednesdays" ;;
4) timer="$timer thursdays" ;;
5) timer="$timer fridays" ;;
6) timer="$timer saturdays" ;;
*) timer="$timer sundays" ;;
esac
;;
montly)
if [ $(date +%m) -eq 1 ]; then
timer="$timer annually"
fi
if [ $(($(date +%m) % 3)) -eq 0 ]; then
timer="$timer quarterly"
elif [ $(($(date +%m) % 6)) -eq 0 ]; then
timer="$timer semiannually"
fi
;;
esac
(
flock --exclusive 200
echo "Pid: $self_pid" >&200 # Store PID in lock file
systemd-cat --priority=notice --identifier=rc.cron -- echo "Launched at $(date '+%Y-%m-%d %H:%M') using timer [$timer]"
for arg in $timer; do
systemd-cat --priority=info --identifier=rc.cron -- echo "Using timer '$arg'"
for file in $script_path/*; do
if [ -x $file ] && echo $file | grep -qe "\.\(sh\|shd\)$"; then
if ! echo $file | grep -q "@" || echo $file | grep -qe "@\(any\|$arg\)\.\(sh\|shd\)$"; then
if echo $file | grep -qe "\.shd$"; then
inode=$(ls -i $file | awk '{print $1}')
# We need the root inode for logging in case of symlinks
find_inode $script_path $inode
systemd-cat --priority=info --identifier=rc.cron -- echo "Starting script $(basename $file) (rc.cron#$find_inode_num) in detached process"
systemctl start cron-detached@$inode:$arg
else
systemd-cat --priority=info --identifier=rc.cron -- echo "Running script $(basename $file) in main process"
if ! systemd-cat --stderr-priority=err --identifier=rc.cron -- $file $arg; then
systemd-cat --priority=alert --identifier=rc.cron -- echo "The script $(basename $file) exited with error $? using timer '$arg'"
fi
fi
fi
fi
done
done
systemd-cat --priority=info --identifier=rc.cron -- echo "Finished running at $(date '+%Y-%m-%d %H:%M')"
) 200>$lock_path/${self_name}.elock
EOF
echo "Installing /etc/systemd/system/cron-startup.service"
sudo tee /etc/systemd/system/cron-startup.service > /dev/null <<EOF
[Unit]
Description=Cronjob running @startup timer
[Service]
Type=forking
ExecStart=/etc/rc.cron startup
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
echo "Installing /etc/systemd/system/cron-shutdown.service"
sudo tee /etc/systemd/system/cron-shutdown.service > /dev/null <<EOF
[Unit]
Description=Cronjob running @shutdown timer
Before=umount.target
DefaultDependencies=no
[Service]
Type=oneshot
ExecStart=/etc/rc.cron shutdown
TimeoutStartSec=0
TimeoutSec=0
[Install]
WantedBy=final.target
EOF
echo "Installing /etc/systemd/system/cron-network.service"
sudo tee /etc/systemd/system/cron-network.service > /dev/null <<EOF
[Unit]
Description=Cronjob running @network timer
Wants=network-online.target
After=network.target network-online.target
[Service]
Type=oneshot
ExecStart=/etc/rc.cron network
[Install]
WantedBy=multi-user.target
EOF
echo "Installing /etc/systemd/system/cron-detached@.service"
sudo tee /etc/systemd/system/cron-detached@.service > /dev/null <<EOF
[Unit]
Description=Cronjob running @%i detached process
[Service]
Type=simple
ExecStart=/etc/rc.cron %i
EOF
echo "Installing /etc/systemd/system/cron@.service"
sudo tee /etc/systemd/system/cron@.service > /dev/null <<EOF
[Unit]
Description=Cronjob running @%i timer
[Service]
Type=oneshot
ExecStart=/etc/rc.cron %i
EOF
echo "Installing /etc/systemd/system/cron@.timer"
sudo tee /etc/systemd/system/cron@.timer > /dev/null <<EOF
[Unit]
Description=Schedule a cronjob %i
[Timer]
Persistent=true
OnCalendar=%i
Unit=cron@%i.service
[Install]
WantedBy=timers.target
EOF
echo "Installing /etc/systemd/system/cron-quarter-hourly.timer"
sudo tee /etc/systemd/system/cron-quarter-hourly.timer > /dev/null <<EOF
[Unit]
Description=Schedule a cronjob quarter-hourly
[Timer]
Persistent=true
OnCalendar=*:0/15
Unit=cron@quarter-hourly.service
[Install]
WantedBy=timers.target
EOF
sudo systemctl daemon-reload
echo "Creating /etc/rc.cron.d/"
test -d /etc/rc.cron.d || sudo mkdir /etc/rc.cron.d
echo "Making /etc/rc.cron executable"
sudo chmod +x /etc/rc.cron
echo "Enaling @startup timer"
sudo systemctl enable -q cron-startup.service
echo "Enaling @shutdown timer"
sudo systemctl enable -q cron-shutdown.service
echo "Enaling @network timer"
sudo systemctl enable -q cron-network.service
echo "Enaling @hourly timer"
sudo systemctl enable -q --now cron@hourly.timer
echo "Enaling @quarter-hourly timer"
sudo systemctl enable -q --now cron-quarter-hourly.timer
echo "Enaling @daily timer"
sudo systemctl enable -q --now cron@daily.timer
echo "Enaling @weekly timer"
sudo systemctl enable -q --now cron@weekly.timer
echo "Enaling @montly timer"
sudo systemctl enable -q --now cron@monthly.timer
echo "Installation is complete"