Skip to content

Instantly share code, notes, and snippets.

@sergioccrr
Last active March 6, 2020 13:49
Show Gist options
  • Save sergioccrr/9456b8bea2f1458ecd34855f44ba4ab6 to your computer and use it in GitHub Desktop.
Save sergioccrr/9456b8bea2f1458ecd34855f44ba4ab6 to your computer and use it in GitHub Desktop.
Script for launch dehydrated (Let's Encrypt) with crontab
#!/bin/bash
set -uo pipefail
IFS=$'\n\t'
# 0 12 * * 1 /etc/dehydrated/cron.dehydrated.sh
# From: https://serverfault.com/a/512780
# From: https://stackoverflow.com/a/41943779
if tty -s; then
exec 5>&1
else
exec 5>/dev/null
fi
if [[ "$EUID" -ne 0 ]]; then
echo "Error! You need to run this as root"
exit 1
fi
cd "$(dirname "$0")" || exit 1
PREFIX="[SSL $(hostname)]"
retOut=$(./dehydrated --cron 2>&1 |tee /dev/fd/5; exit "${PIPESTATUS[0]}")
retVal=$?
if [ $retVal -ne 0 ]; then
echo "$retOut" | mail -s "$PREFIX Error Let's Encrypt" root
exit 1
fi
TOTAL=$(echo "$retOut" | grep -c 'Renewing!')
if [ "$TOTAL" -eq 0 ]; then
exit 0
fi
retOut=$(systemctl reload nginx 2>&1 |tee /dev/fd/5; exit "${PIPESTATUS[0]}")
retVal=$?
if [ $retVal -ne 0 ]; then
echo "$retOut" | mail -s "$PREFIX Error Nginx" root
exit 1
fi
retOut=$(./dehydrated --cleanup 2>&1 |tee /dev/fd/5; exit "${PIPESTATUS[0]}")
retVal=$?
if [ $retVal -ne 0 ]; then
echo "$retOut" | mail -s "$PREFIX Warning cleanup" root
exit 1
fi
echo "$TOTAL domains renewed" | mail -s "$PREFIX Success!" root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment