diff --git a/README.md b/README.md index 111e359..3521b03 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 ed9dd08..952adab 100755 --- a/shepherd +++ b/shepherd @@ -133,7 +133,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="" @@ -181,6 +181,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" @@ -190,4 +191,25 @@ main() { fi } +run_crond() { + mkdir -p /etc/cron.d + 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='' + 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 "$@"