Automating Tasks with Cron Jobs
Cron jobs allow you to schedule commands or scripts to run automatically at specified intervals. They are useful for tasks such as sending scheduled emails, running database backups, clearing cache files, or triggering WordPress cron events.
Accessing Cron Job Management
- Log in to DirectAdmin.
- Navigate to Advanced Features.
- Click on Cron Jobs.
Creating a Cron Job
- On the Cron Jobs page, you will see fields for scheduling and the command to run.
- Set the schedule using the five time fields: Minute, Hour, Day of Month, Month, and Day of Week.
- Enter the full command to execute in the Command field.
- Click Add or Save to create the cron job.
Cron Schedule Syntax
* * * * * command_to_run
│ │ │ │ │
│ │ │ │ └── Day of Week (0-7, 0 and 7 = Sunday)
│ │ │ └──── Month (1-12)
│ │ └────── Day of Month (1-31)
│ └──────── Hour (0-23)
└────────── Minute (0-59)
Common Schedule Examples
*/5 * * * *- Every 5 minutes.0 * * * *- Every hour (at the top of the hour).0 0 * * *- Once daily at midnight.0 0 * * 0- Once weekly on Sunday at midnight.0 0 1 * *- Once monthly on the 1st at midnight.
Example Cron Commands
# Run a PHP script
/usr/local/bin/php /home/username/domains/example.com/public_html/cron.php
# WordPress cron
/usr/local/bin/php /home/username/domains/example.com/public_html/wp-cron.php
# Database backup
/usr/local/bin/mysqldump -u dbuser -pDBPASS dbname > /home/username/backups/db_backup.sql
Always use the full path to commands and scripts in cron jobs. Cron runs in a minimal environment and may not have the same PATH as your login shell.
Add
>/dev/null 2>&1 at the end of your cron command to suppress email notifications for successful runs. Without this, cron will send an email every time the job executes.