1
0
Fork 0
mirror of https://github.com/yavook/kiwi-cron.git synced 2024-11-21 15:03:01 +00:00
kiwi-cron/cron-exec

151 lines
3.4 KiB
Bash
Executable file

#!/bin/sh
crontab_append () {
crontab="$(
printf '%s\n%s' \
"${crontab}" "${1}"
)"
}
print_cron_schedule() {
cas_min="${1}"
cas_hour="${2}"
cas_day="${3}"
cas_month="${4}"
cas_weekday="${5}"
cas_command="${6}"
printf '%-8s%-8s%-8s%-8s%-8s%s\n' \
"${cas_min}" "${cas_hour}" "${cas_day}" "${cas_month}" "${cas_weekday}" "${cas_command}"
}
crontab='# crontab generated for kiwi-cron'
crontab_append '# generation time: '"$(date)"
crontab_append '#'
crontab_append "$(
print_cron_schedule \
"# min" "hour" "day" "month" "weekday" "command"
)"
# check *ly dirs in root directory
schedule_dirs="$(
find "/kiwi-cron" -type d -name "*ly" -mindepth 1 -maxdepth 1
)"
for schedule_dir in ${schedule_dirs}; do
# count files in scheduler directory
schedule_filecount="$(
find "${schedule_dir}" -type f -mindepth 1 -maxdepth 1 \
| wc -l
)"
# ignore if empty
if [ "${schedule_filecount}" -eq "0" ]; then
continue
fi
# infer cron schedule by directory name
units="${schedule_dir##*/}"
case "${units}" in
hourly)
crontab_append "$(
print_cron_schedule \
"0" "*" "*" "*" "*" "run-parts '${schedule_dir}'"
)"
;;
daily)
crontab_append "$(
print_cron_schedule \
"0" "2" "*" "*" "*" "run-parts '${schedule_dir}'"
)"
;;
weekly)
crontab_append "$(
print_cron_schedule \
"0" "3" "*" "*" "6" "run-parts '${schedule_dir}'"
)"
;;
monthly)
crontab_append "$(
print_cron_schedule \
"0" "5" "1" "*" "*" "run-parts '${schedule_dir}'"
)"
;;
yearly|annually)
crontab_append "$(
print_cron_schedule \
"0" "0" "1" "1" "*" "run-parts '${schedule_dir}'"
)"
;;
esac
done
# check dirs in "every" subdirectory
every_schedule_dirs="$(
find "/kiwi-cron/every" -type d -mindepth 1 -maxdepth 1
)"
for schedule_dir in ${every_schedule_dirs}; do
# count files in scheduler directory
schedule_filecount="$(
find "${schedule_dir}" -type f -mindepth 1 -maxdepth 1 \
| wc -l
)"
# ignore if empty
if [ "${schedule_filecount}" -eq "0" ]; then
continue
fi
# infer cron schedule by directory name
schedule="${schedule_dir##*/}"
every="$( echo "${schedule}" | awk -F_ '{print $1}' )"
units="$( echo "${schedule}" | awk -F_ '{print $2}' )"
units="${units%s}"
# generate valid random values
rand_min=$(( RANDOM % 60 ))
# rand_hour=$(( RANDOM % 24 ))
# generating nighttime values seems like a good idea!
rand_hour=$(( (RANDOM % 7 + 21) % 24 ))
rand_day=$(( RANDOM % 31 + 1 ))
case "${units}" in
minute)
crontab_append "$(
print_cron_schedule \
"*/${every}" "*" "*" "*" "*" "run-parts '${schedule_dir}'"
)"
;;
hour)
crontab_append "$(
print_cron_schedule \
"${rand_min}" "*/${every}" "*" "*" "*" "run-parts '${schedule_dir}'"
)"
;;
day)
crontab_append "$(
print_cron_schedule \
"${rand_min}" "${rand_hour}" "*/${every}" "*" "*" "run-parts '${schedule_dir}'"
)"
;;
month)
crontab_append "$(
print_cron_schedule \
"${rand_min}" "${rand_hour}" "${rand_day}" "*/${every}" "*" "run-parts '${schedule_dir}'"
)"
;;
esac
done
# replace crontab
echo "${crontab}" | crontab -
exec crond -fd 8