Create a shell script file renew_cert.sh and grant the permission to execute the shell script
touch /var/www/renew_cert.sh
chmod 777 /var/www/renew_cert.sh
In your favorite editor nano /var/www/renew_cert.sh edit file and write the following content
#!/bin/bash
# Define domain and certificate paths
DOMAIN="subdomain.example.com"
CERT_PATH="/etc/letsencrypt/live/$DOMAIN/fullchain.pem"
# Get the number of remaining days before expiry
REMAINING_DAYS=$(openssl x509 -enddate -noout -in $CERT_PATH | sed 's/.*=\(.*\)/\1/' | xargs -I {} date -d {} +%s)
CURRENT_DATE=$(date +%s)
DAYS_LEFT=$(( ($REMAINING_DAYS - $CURRENT_DATE) / 86400 ))
# Check if remaining days is less than 5
if [ $DAYS_LEFT -lt 5 ]; then
echo "Certificate is about to expire in $DAYS_LEFT days. Renewing..."
# Renew certificate using certbot
certbot renew
# Check if renewal was successful
if [ $? -eq 0 ]; then
echo "SSL certificate renewed successfully."
# Restart pm2 application with ID 0
echo "Restart pm2 application with ID 0"
pm2 restart 0
# Reload HTTP configuration in Asterisk
echo "Reload HTTP configuration in Asterisk"
asterisk -rx 'reload http'
else
echo "Certbot renewal failed. Please check your Certbot logs."
fi
else
echo "SSL certificate is valid for more than 5 days. No renewal needed."
fi
add the script to your cronjob of your OS