From 834aad50d3c6e2b817ccf59931a76504b27229f6 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Mon, 27 Jun 2022 20:59:49 -0700 Subject: [PATCH 1/2] run shepherd via cron --- README.md | 2 +- shepherd | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ad11482..7e35d7c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ A Docker swarm service for automatically updating your services whenever their b ### Configuration -Shepherd will try to update your services every 5 minutes by default. You can adjust this value using the `SLEEP_TIME` variable. +Shepherd will try to update your services every 5 minutes by default. You can adjust this value using the `SLEEP_TIME` variable. Alternatively set `CRON_SETTING` to run shepherd at a given time. When `CRON_SETTING` is provided, `SLEEP_TIME` will be ignored. You can prevent services from being updated by appending them to the `IGNORELIST_SERVICES` variable. This should be a space-separated list of service names. diff --git a/shepherd b/shepherd index 9c288c3..9d3429a 100755 --- a/shepherd +++ b/shepherd @@ -107,7 +107,7 @@ get_registry_password() { fi } -main() { +run_main() { local sleep_time supports_detach_option supports_registry_auth verbose image_autoclean_limit ignorelist local registry_user="" local registry_password="" @@ -155,6 +155,7 @@ main() { if [[ ${RUN_ONCE_AND_EXIT+x} ]]; then update_services "$ignorelist" "$supports_detach_option" "$supports_registry_auth" "$supports_insecure_registry" "$supports_no_resolve_image" "$image_autoclean_limit" "$registry_user" "$registry_password" "$registry_host" + logger "Update finished." "true" else while true; do update_services "$ignorelist" "$supports_detach_option" "$supports_registry_auth" "$supports_insecure_registry" "$supports_no_resolve_image" "$image_autoclean_limit" "$registry_user" "$registry_password" "$registry_host" @@ -164,4 +165,25 @@ main() { fi } +run_crond() { + mkdir -p /etc/cron.d + echo "${CRON_SETTING} BASH_ENV=/save-env /shepherd >> /var/log/shepherd.log" > /etc/cron.d/shepherd + chmod 0644 /etc/cron.d/shepherd + export RUN_ONCE_AND_EXIT='true' + export CRON_SETTING='' + env > /save-env + crontab /etc/cron.d/shepherd + touch /var/log/cron.log + touch /var/log/shepherd.log + crond -L /var/log/cron.log && tail -f /var/log/cron.log -f /var/log/shepherd.log +} + +main() { + if [[ -z "${CRON_SETTING}" ]]; then + run_main "$@" + else + run_crond "$@" + fi +} + main "$@" From 93619793ad9b356e7252c5f9d9eb9724ceace3cb Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Sat, 24 Sep 2022 14:07:41 -0700 Subject: [PATCH 2/2] resolve conversation from djmaze --- shepherd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shepherd b/shepherd index 2ae0d43..952adab 100755 --- a/shepherd +++ b/shepherd @@ -193,7 +193,7 @@ run_main() { run_crond() { mkdir -p /etc/cron.d - echo "${CRON_SETTING} BASH_ENV=/save-env /shepherd >> /var/log/shepherd.log" > /etc/cron.d/shepherd + echo "${CRON_SETTING:-} BASH_ENV=/save-env /usr/local/bin/shepherd >> /var/log/shepherd.log 2>&1" > /etc/cron.d/shepherd chmod 0644 /etc/cron.d/shepherd export RUN_ONCE_AND_EXIT='true' export CRON_SETTING='' @@ -205,7 +205,7 @@ run_crond() { } main() { - if [[ -z "${CRON_SETTING}" ]]; then + if [[ -z "${CRON_SETTING:-}" ]]; then run_main "$@" else run_crond "$@"