From 0cd472a08f556c0783cdb0db124dfe1fbea5cc5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 1 Mar 2022 18:40:05 +0100 Subject: [PATCH] modularity --- bin/kiwi-cron | 10 +++----- libexec/kiwi-cron/create_schedule | 36 ++++++++++++++-------------- libexec/kiwi-cron/run_cron | 40 +++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 libexec/kiwi-cron/run_cron diff --git a/bin/kiwi-cron b/bin/kiwi-cron index bbc3af9..c1e2707 100755 --- a/bin/kiwi-cron +++ b/bin/kiwi-cron @@ -1,9 +1,5 @@ #!/bin/sh -# replace crontab -/usr/local/libexec/kiwi-cron/schedule_dirs \ - | /usr/local/libexec/kiwi-cron/print_crontab \ - | crontab - - -# run crond -exec crond -fd 8 \ No newline at end of file +exec /usr/local/libexec/kiwi-cron/run_cron \ + "${@}" \ + /usr/local/libexec/kiwi-cron/schedule_dirs diff --git a/libexec/kiwi-cron/create_schedule b/libexec/kiwi-cron/create_schedule index 863b5b4..34ed88c 100755 --- a/libexec/kiwi-cron/create_schedule +++ b/libexec/kiwi-cron/create_schedule @@ -32,22 +32,22 @@ cs_rand_weekday=$(( (6 + RANDOM % 2) % 7 )) cs_rand_month=$(( RANDOM % 2 + 1 )) case "${cs_units}" in -minute) - echo "*${cs_every}" "*" "*" "*" "*" "${cs_command}" - ;; -hour) - echo "${cs_rand_min}" "*${cs_every}" "*" "*" "*" "${cs_command}" - ;; -day) - echo "${cs_rand_min}" "${cs_rand_hour}" "*${cs_every}" "*" "*" "${cs_command}" - ;; -week) - echo "${cs_rand_min}" "${cs_rand_hour}" "*" "*" "${cs_rand_weekday}" "${cs_command}" - ;; -month) - echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "*${cs_every}" "*" "${cs_command}" - ;; -year) - echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "${cs_rand_month}" "*" "${cs_command}" - ;; + minute) + echo "*${cs_every}" "*" "*" "*" "*" "${cs_command}" + ;; + hour) + echo "${cs_rand_min}" "*${cs_every}" "*" "*" "*" "${cs_command}" + ;; + day) + echo "${cs_rand_min}" "${cs_rand_hour}" "*${cs_every}" "*" "*" "${cs_command}" + ;; + week) + echo "${cs_rand_min}" "${cs_rand_hour}" "*" "*" "${cs_rand_weekday}" "${cs_command}" + ;; + month) + echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "*${cs_every}" "*" "${cs_command}" + ;; + year) + echo "${cs_rand_min}" "${cs_rand_hour}" "${cs_rand_day}" "${cs_rand_month}" "*" "${cs_command}" + ;; esac diff --git a/libexec/kiwi-cron/run_cron b/libexec/kiwi-cron/run_cron new file mode 100644 index 0000000..347aabb --- /dev/null +++ b/libexec/kiwi-cron/run_cron @@ -0,0 +1,40 @@ +#!/bin/sh + +# check for CLI options +while getopts ':n' rc_optarg; do + case "${rc_optarg}" in + n) + rc_dry_run="yes" + ;; + + *) + echo "Invalid option: -${OPTARG}." + exit 2 + esac +done + +shift "$(( OPTIND - 1 ))" + +# CLI arguments +rc_executable="${1}" + + +if [ -n "${rc_dry_run}" ]; then + # verbose, dry run + set -x + + "${rc_executable}" \ + | /usr/local/libexec/kiwi-cron/print_crontab + + exit 0 +fi + +# replace crontab +"${rc_executable}" \ + | /usr/local/libexec/kiwi-cron/print_crontab \ + | crontab - + +# hand over to crond +exec crond -fd 8 + +exit 0 \ No newline at end of file