-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart-cron.sh
More file actions
executable file
·61 lines (51 loc) · 1.51 KB
/
start-cron.sh
File metadata and controls
executable file
·61 lines (51 loc) · 1.51 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
#!/bin/bash
stop_cron_daemon() {
local cron_pid="$1";
if [ -n "$cron_pid" ] && kill -0 "$cron_pid" &>/dev/null; then
kill "$cron_pid";
else
exit 0
fi
}
cron_monitored_files() {
find /var/spool/cron/crontabs \
/etc/cron.d \
/etc/cron.hourly \
/etc/cron.daily \
/etc/cron.weekly \
/etc/cron.monthly \
-mindepth 1 -not -name .placeholder
}
[ "$DEBUG" = 1 ] && set -x;
: "${RUN_USER:=root}"
# make sure RUN_USER exists and is setup correctly
if ! cron-user check "$RUN_USER"; then
exit 1;
fi
# setup crontab
/usr/bin/crontab -u "${RUN_USER}" /crontab.txt;
# setup file permissions expected by crond
chmod go-rwx "/var/spool/cron/crontabs/${RUN_USER}";
chown "${RUN_USER}:crontab" "/var/spool/cron/crontabs/${RUN_USER}";
# capture environment variables which can be used in scripts
for var in $PRESERVE_ENV_VARS; do
echo "$var=$(eval echo \$$var)"
done > /etc/environment
# setup a background subshell that would touch crontabs so
# that they are correctly loaded; work aroud for some quirks
# with vixie cron
(
sleep 5;
while read -r f; do
touch "$f";
done < <(cron_monitored_files)
)&
# Setup a trap to kill the background process
trap 'stop_cron_daemon $cron_pid' INT TERM EXIT
# start cron in foreground mode and background it using
# shell semantics so that the PID can be captured
/usr/sbin/cron -f -L15 &
# capture the PID of the background process
cron_pid=$!
# wait for the background process to finish
wait $cron_pid