Automating Tasks with Cron Jobs
Cron jobs allow you to schedule commands or scripts to run automatically at specified intervals. They are essential for tasks like sending scheduled emails, running database maintenance, generating reports, or triggering WordPress scheduled tasks.
Setting Up a Cron Job in DirectAdmin
- Log in to your DirectAdmin control panel.
- Navigate to Advanced Features > Cron Jobs.
- Click Create Cron Job.
- Set the schedule using the five time fields:
- Minute (0–59)
- Hour (0–23)
- Day of Month (1–31)
- Month (1–12)
- Day of Week (0–6, where 0 is Sunday)
- Enter the command to execute.
- Click Save.
Common Cron Job Examples
- WordPress cron (every 15 minutes):
*/15 * * * * /usr/local/bin/php /home/username/domains/yourdomain.com/public_html/wp-cron.php - Database backup (daily at 2:00 AM):
0 2 * * * /usr/local/bin/mysqldump -u dbuser -pPassword dbname > /home/username/backups/db_backup.sql - Clear temp files (weekly on Sunday):
0 3 * * 0 rm -rf /home/username/tmp/*
Cron Schedule Shortcuts
* * * * *– Every minute0 * * * *– Every hour0 0 * * *– Every day at midnight0 0 * * 0– Every Sunday at midnight0 0 1 * *– First day of every month
Best Practices
- Always use full paths for both the interpreter and the script.
- Redirect output to a log file or to
/dev/nullto avoid receiving cron emails:command > /dev/null 2>&1 - Test your command manually via SSH before adding it as a cron job.
- Avoid running cron jobs too frequently on shared hosting as this can impact server resources.