move interval management to a util funcion

This commit is contained in:
Bruno Zell 2018-09-28 17:07:33 +02:00
parent 1aa799bb70
commit 96897f8e37
2 changed files with 17 additions and 9 deletions

View File

@ -24,23 +24,18 @@ done
echo "Done with startup"
last_sync_file="/etc/letsencrypt/last_sync.txt"
one_week_sec=604800
# Instead of trying to run `cron` or something like that, just sleep and run `certbot`.
while [ true ]; do
last_sync_sec=$(stat -c %Y "$last_sync_file")
now_sec=$(date -d now +%s)
runned_sec=$(( ($now_sec - $last_sync_sec) ))
is_finshed_week_sec=$(( ($one_week_sec - $runned_sec) ))
echo "Not run_certbot.sh"
if [ ! -e "$last_sync_file" ] || [ $is_finshed_week_sec -lt 0 ]; then
# recreate the file
if [ is_sync_required $last_sync_file ]; then
# recreate the file to persist the last sync timestamp
touch "$last_sync_file"
# run certbot to request all the ssl certs we can find
echo "Run certbot"
/scripts/run_certbot.sh
else
echo "Not run certbot"
fi
# Sleep for 1 week

View File

@ -75,3 +75,16 @@ get_certificate() {
$letsencrypt_url -d $1 --http-01-port 1337 \
--standalone --preferred-challenges http-01 --debug
}
is_sync_required() {
if [ ! -e "$1" ]; then
return true
fi
one_week_sec=604800
last_sync_sec=$(stat -c %Y "$1")
now_sec=$(date -d now +%s)
last_sync_delta_sec=$(( ($now_sec - $last_sync_sec) ))
is_finshed_week_sec=$(( ($one_week_sec - $last_sync_delta_sec) ))
return $is_finshed_week_sec -lt 0
}