Last active
March 6, 2020 13:49
-
-
Save sergioccrr/9456b8bea2f1458ecd34855f44ba4ab6 to your computer and use it in GitHub Desktop.
Script for launch dehydrated (Let's Encrypt) with crontab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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