This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-cron
executable file
·59 lines (51 loc) · 1.59 KB
/
start-cron
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
#!/usr/bin/env bash
# make link to custom location of /etc/cron.d if provided
if [ "${CRON_PATH}" ]; then
rm -rf /etc/cron.d
ln -sfTv "${CRON_PATH}" /etc/cron.d
fi
# remove write permission for (g)roup and (o)ther (required by cron)
chmod -R go-w /etc/cron.d
# setting user for additional cron jobs
case $1 in
-u=*|--user=*)
crontab_user="-u ${1#*=}"
shift
;;
-u|--user)
crontab_user="-u $2"
shift 2
;;
-*)
echo "Unknown option: ${1%=*}" > /dev/stderr
exit 1
;;
*)
crontab_user=""
;;
esac
# adding additional cron jobs passed by arguments
# every job must be a single quoted string and have standard crontab format,
# e.g.: start-cron --user user "0 \* \* \* \* env >> /var/log/cron.log 2>&1"
{ for cron_job in "$@"; do echo -e ${cron_job}; done } \
| sed --regexp-extended 's/\\(.)/\1/g' \
| crontab ${crontab_user} -
# update default values of PAM environment variables (used by CRON scripts)
env | while read -r line; do # read STDIN by line
# split LINE by "="
IFS="=" read var val <<< ${line}
# remove existing definition of environment variable, ignoring exit code
sed --in-place "/^${var}[[:blank:]=]/d" /etc/security/pam_env.conf || true
# append new default value of environment variable
echo "${var} DEFAULT=\"${val}\"" >> /etc/security/pam_env.conf
done
# start cron
service cron start
# trap SIGINT and SIGTERM signals and gracefully exit
trap "service cron stop; kill \$!; exit" SIGINT SIGTERM
# start "daemon"
while true
do
# watch /var/log/cron.log restarting if necessary
cat /var/log/cron.log & wait $!
done