Setting Up a Weekly Cron Job for Pi-hole Gravity Updates
Setting Up a Weekly Cron Job for Pi-hole Gravity Updates
In this blog post, I’ll walk you through the process of setting up a weekly cron job to send an SMS notification when the Pi-hole gravity update is completed. This setup is particularly useful if you’re running Pi-hole in a Docker container and want to ensure that your cron job persists across container restarts.
Prerequisites
- A running Pi-hole instance in a Docker container.
- Access to the host machine where Docker is running.
- Basic knowledge of shell scripting and cron jobs.
Step 1: Create the Shell Script
First, create a shell script on the host machine that will be executed by the cron job. This script will update the Pi-hole gravity database and send an SMS notification upon successful completion.
-
Create the Script:
Open a text editor and create a new file named
pihole_update_gravity_with_sms.sh
:sudo nano /usr/local/bin/pihole_update_gravity_with_sms.sh
-
Add the Script Content:
Copy and paste the following content into the file:
#!/bin/bash # Path to the Pi-hole gravity update script GRAVITY_SCRIPT="docker exec pihole /usr/local/bin/pihole updateGravity" # SMS API credentials SMS_USER="USERNAME" SMS_PASS="APIKEY" SMS_MESSAGE="Pi-hole Gravity Update Completed Successfully." echo "Starting gravity update..." # Execute the gravity update \$GRAVITY_SCRIPT # Check if the gravity update was successful if [ \$? -eq 0 ]; then echo "Gravity update completed successfully." # Send notification via SMS using the confirmed working syntax echo "Sending SMS notification..." /usr/bin/curl "https://smsapi.free-mobile.fr/sendmsg?user=\${SMS_USER}&pass=\${SMS_PASS}&msg=\${SMS_MESSAGE}" echo "SMS notification sent successfully." else echo "Gravity update failed. No SMS notification sent." fi
-
Make the Script Executable:
Change the permissions of the script to make it executable:
sudo chmod +x /usr/local/bin/pihole_update_gravity_with_sms.sh
Step 2: Add the Script to the Host’s Cron Schedule
Next, add the script to the host’s cron schedule to run it weekly at 5:30 AM every Monday.
-
Edit the Crontab:
Open the crontab file for editing:
sudo crontab -e
-
Add the Cron Job Entry:
Add the following line to the crontab file to run the script at 5:30 AM every Monday:
30 5 * * 1 /usr/local/bin/pihole_update_gravity_with_sms.sh
-
Save and Exit:
Save the changes and exit the crontab editor.
Step 3: Verify the Cron Job
To ensure that the cron job is set up correctly, you can manually trigger the script and check the logs for any output or errors.
-
Manually Run the Script:
Execute the script manually to verify that it works as expected:
sudo /usr/local/bin/pihole_update_gravity_with_sms.sh
-
Check the Logs:
Review the system logs to see if the script was executed and if there were any errors:
bash sudo tail -f /var/log/syslog
Conclusion
By following these steps, you have successfully set up a weekly cron job to send an SMS notification upon the completion of the Pi-hole gravity update. This setup ensures that you are promptly notified of the update status, helping you maintain the efficiency of your Pi-hole instance.