Cron jobs let you schedule commands to run automatically at specified intervals. This is useful for backups, cleanup scripts, sending reports, and replacing WordPress wp-cron.
Cron Schedule Syntax
A cron schedule has five fields:
* * * * * command
| | | | |
| | | | +-- Day of week (0-7, Sunday = 0 or 7)
| | | +---- Month (1-12)
| | +------ Day of month (1-31)
| +-------- Hour (0-23)
+---------- Minute (0-59)
Common Schedule Patterns
| Pattern | Schedule | Expression |
|---|---|---|
| Every minute | Once per minute | * * * * * |
| Every 5 minutes | 12 times per hour | */5 * * * * |
| Every hour | Once per hour | 0 * * * * |
| Every day at midnight | Once per day | 0 0 * * * |
| Every day at 3:00 AM | Once per day | 0 3 * * * |
| Every Monday at 9 AM | Once per week | 0 9 * * 1 |
| First of month at midnight | Once per month | 0 0 1 * * |
| Every 6 hours | 4 times per day | 0 */6 * * * |
Creating a Cron Job in cPanel
- Log in to cPanel
- Go to Advanced > Cron Jobs
- Under Add New Cron Job, select a common setting or enter custom values
- Enter the command to run
- Click Add New Cron Job
Useful Examples
Run a PHP Script
/usr/local/bin/php /home/username/public_html/scripts/cleanup.php
Database Backup (mysqldump)
/usr/bin/mysqldump -u dbuser -p'dbpassword' dbname > /home/username/backups/db-backup-$(date +%Y%m%d).sql
Fetch a URL (Trigger a Web Script)
/usr/bin/curl -s https://yourdomain.com/cron-task.php > /dev/null 2>&1
Delete Files Older Than 30 Days
/usr/bin/find /home/username/backups/ -type f -name "*.sql" -mtime +30 -delete
Replace WordPress wp-cron with a Real Cron Job
WordPress uses wp-cron.php which only runs when someone visits your site. This is unreliable for scheduled posts, email queues, and plugin tasks. Replace it with a server cron job.
Step 1: Disable wp-cron
Add this to wp-config.php:
define( 'DISABLE_WP_CRON', true );
Step 2: Create a Real Cron Job
In cPanel Cron Jobs, add:
*/15 * * * * /usr/local/bin/php /home/username/public_html/wp-cron.php > /dev/null 2>&1
Or using curl:
*/15 * * * * /usr/bin/curl -s https://yourdomain.com/wp-cron.php > /dev/null 2>&1
This runs wp-cron every 15 minutes regardless of site traffic.
Email Notifications
By default, cron sends output to your cPanel email address. Configure this at the top of the Cron Jobs page under Cron Email.
Suppress Emails for a Specific Job
Append > /dev/null 2>&1 to the end of the command:
/usr/local/bin/php /home/username/script.php > /dev/null 2>&1
Log Output to a File Instead
/usr/local/bin/php /home/username/script.php >> /home/username/logs/cron.log 2>&1
Troubleshooting
- Cron not running: Verify the command works by running it manually in Terminal (cPanel > Terminal) first.
- Permission denied: Ensure the script is executable:
chmod +x script.sh - Wrong PHP version: Use the full path to the specific PHP version:
/usr/local/bin/ea-php82instead of/usr/local/bin/php - Script not found: Always use absolute paths, not relative paths.
- No output: The command might be working silently. Check if the expected result (backup file, database change, etc.) is present.