Skip to content

Instantly share code, notes, and snippets.

@bomsn
Created January 21, 2021 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bomsn/e7ad971778172ede909b8995cc7cd061 to your computer and use it in GitHub Desktop.
Save bomsn/e7ad971778172ede909b8995cc7cd061 to your computer and use it in GitHub Desktop.
Sample code for cron job automation ( Create a certbot auto-renewal cron job )
#!/bin/bash
# If our cron job already exists, bail out.
if ! crontab -l &> /dev/null | grep -q "/usr/bin/certbot renew"; then
# Copy the existing cron jobs into a temporary file
crontab -l &> /dev/null > cronjobs.txt
# Add our new cron job to the file
echo "15 3 * * * /usr/bin/certbot renew --quiet" >> cronjobs.txt
# Replace content of current user crontab with the content from our cron jobs file
cat cronjobs.txt > /var/spool/cron/crontabs/"$USER"
# Remove the temporary file
rm cronjobs.txt
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment