Comprehensive Guide to Setting Up Cron Jobs in Ubuntu and Frappe Including SSL Renewal on a DigitalOcean Droplet
Comprehensive Guide to Setting Up Cron Jobs in Ubuntu and Frappe Including SSL Renewal on a DigitalOcean Droplet
Understanding Cron Jobs in Linux
Cron jobs are scheduled tasks executed automatically at specified times and dates. In Linux, cron schedules are defined in the format:
* * * * * command to execute
Each asterisk represents: - Minute (0-59) - Hour (0-23) - Day of Month (1-31) - Month (1-12) - Day of Week (0-6) (Sunday=0)
Examples:
10 14 * * 1
: Executes at 2:10 PM every Monday.0 3 * * 0
: Executes at 3:00 AM every Sunday.
Setting Up Cron Jobs on Ubuntu
Step 1: Accessing Cron Jobs
Open the crontab editor:
sudo crontab -e
Choose your preferred text editor (nano, vim).
Step 2: Adding a Cron Job
To schedule a script or command, add a new line in the editor:
10 14 * * 1 /path/to/script.sh >> /path/to/logfile.log 2>&1
This runs the script every Monday at 2:10 PM, logging output and errors to logfile.log
.
Step 3: Saving and Exiting
- Nano: Press
Ctrl+O
(save), thenCtrl+X
(exit). - Vim: Press
Esc
, type:wq
, and press Enter.
Setting Up Cron Jobs in Frappe
Frappe allows scheduling tasks using the bench environment.
Step 1: Create Scheduled Tasks
In Frappe apps, scheduled tasks are defined in the hooks.py
file:
scheduler_events = {
"daily": [
"your_app.tasks.daily_task"
],
"weekly": [
"your_app.tasks.weekly_task"
]
}
Step 2: Implement Tasks in Python
In your app (tasks.py
):
import frappe
def daily_task():
frappe.logger().info("Running daily task")
def weekly_task():
frappe.logger().info("Running weekly task")
Frappe automatically manages execution according to your defined schedule.
Renewing SSL Certificates Automatically in Frappe (DigitalOcean Droplet)
Step 1: Setting Up Cron Job for SSL Renewal
Open the root crontab:
sudo crontab -e
Add the following cron job to renew SSL certificates weekly (every Sunday at 3 AM):
0 3 * * 0 cd /home/frappe/frappe-bench && /usr/local/bin/bench renew-lets-encrypt >> /home/frappe/frappe-bench/logs/letsencrypt-renew.log 2>&1
Explanation:
cd /home/frappe/frappe-bench
: Navigates to the Frappe bench directory./usr/local/bin/bench renew-lets-encrypt
: Runs SSL renewal.- Logs output/errors to
letsencrypt-renew.log
for troubleshooting.
Step 2: Verifying Bench Command Path
Ensure the bench
command path is correct:
which bench
Usually returns /usr/local/bin/bench
. Adjust if needed.
Step 3: Testing Manually
Test the SSL renewal manually:
cd /home/frappe/frappe-bench
sudo bench renew-lets-encrypt
Ensure successful execution.
Step 4: Checking SSL Certificate Validity
Use the following command to verify the validity period of your SSL certificates:
sudo openssl x509 -enddate -noout -in /etc/letsencrypt/live/yourdomain.com/fullchain.pem
Replace yourdomain.com
with your actual domain.
🔁 Alternative to certbot
for Checking SSL Expiry
If certbot
is unavailable or throws an error, you can check the SSL certificate expiry date using openssl
:
echo | openssl s_client -servername yourdomain.com -connect yourdomain.com:443 2>/dev/null | openssl x509 -noout -enddate
Cron Job Troubleshooting
- Check Logs: Regularly check logs in
/home/frappe/frappe-bench/logs/
. - Permission Issues: Always use absolute paths and correct user permissions.
By following these guidelines, you ensure automated, reliable task execution and SSL certificate management for your Ubuntu-based Frappe application hosted on DigitalOcean.