đ Table of Contents
What is a Cron Job and Why Does It Matter
A Cron Job is a time-based task scheduler on Linux servers that automatically executes scripts or commands at specified intervals. The name comes from Chronos (Greek god of time), and "crontab" is the configuration file that stores the schedule.
On web hosting, Cron Jobs are invaluable for automating repetitive tasks â backing up a database every night, sending weekly reports, clearing cache files, or syncing data from external APIs without any manual intervention.
- Automated MySQL database backup every night at 02:00
- Send email newsletter every Monday at 09:00
- Delete session/tmp files older than 7 days
- Fetch data from external APIs every 30 minutes
- Regenerate sitemap.xml every 6 hours
On AsiaGB Hosting powered by DirectAdmin, you can configure Cron Jobs directly from the control panel GUI â no SSH or manual crontab editing required.
Understanding Cron Syntax
Cron syntax consists of 5 time fields followed by the command to execute. Each field represents a different time unit:
* * * * * /path/to/command
â â â â â
â â â â âââ Day of week (0-7) (0 and 7 = Sunday)
â â â âââââ Month (1-12)
â â âââââââ Day of month (1-31)
â âââââââââ Hour (0-23)
âââââââââââ Minute (0-59)
Special Characters in Cron
*= every value (wildcard)*/n= every n units, e.g.*/5= every 5 minutesn,m= multiple values, e.g.1,15= 1st and 15thn-m= range, e.g.9-17= hours 9 through 17
Setting Up Cron Jobs in DirectAdmin
Configuring a Cron Job in DirectAdmin is straightforward. Follow these steps:
- Log in to DirectAdmin Panel at
yourdomain.com:2222 - Navigate to Advanced Features
- Click Cron Jobs
- Fill in the Minute, Hour, Day, Month, Weekday fields
- Enter your command in the Command field
- Click Add to save
>/dev/null 2>&1 to your command to suppress all output.
Running PHP Scripts via Cron
There are two main ways to execute PHP scripts from Cron: PHP CLI and curl/wget:
Method 1: PHP CLI (Recommended)
/usr/local/bin/php /home/USERNAME/public_html/cron/backup.php
Replace USERNAME with your DirectAdmin account name and adjust the script path accordingly.
Method 2: Curl to URL
curl -s https://yourdomain.com/cron/backup.php > /dev/null 2>&1
Simpler but be mindful of security since the URL is publicly accessible.
Finding PHP Path on DirectAdmin
If you're unsure about the PHP binary path, common locations are:
- PHP 8.x:
/usr/local/bin/php - PHP 7.4:
/usr/local/bin/php74
Common Use Cases on Hosting
1. Nightly MySQL Database Backup
0 2 * * * /usr/local/bin/php /home/USERNAME/public_html/backup-db.php
Runs every day at 02:00 â ideal for low-traffic hours.
2. Weekly Email Newsletter
0 9 * * 1 /usr/local/bin/php /home/USERNAME/public_html/send-newsletter.php
Runs every Monday at 09:00.
3. Clear Old Cache Files
0 */6 * * * /usr/bin/find /home/USERNAME/public_html/cache/ -mtime +7 -delete
Runs every 6 hours, deletes files older than 7 days.
4. WordPress WP-Cron
*/15 * * * * /usr/local/bin/php /home/USERNAME/public_html/wp-cron.php > /dev/null 2>&1
Run WP-Cron every 15 minutes. Recommended to disable WordPress's built-in WP-Cron and use this server-side alternative for better performance.
5. Regenerate Sitemap
0 */6 * * * /usr/local/bin/php /home/USERNAME/public_html/sitemap-generate.php
Regenerate sitemap every 6 hours so search engines always get fresh data.
Cron Syntax Reference Table
| Cron Expression | Meaning | Use Case |
|---|---|---|
* * * * * | Every minute | Monitoring / health check |
*/5 * * * * | Every 5 minutes | API polling |
*/15 * * * * | Every 15 minutes | WP-Cron |
0 * * * * | Every hour (at :00) | Cache clear |
0 */6 * * * | Every 6 hours | Sitemap regen |
0 2 * * * | Daily at 02:00 | Daily backup |
0 9 * * 1 | Monday at 09:00 | Weekly report |
0 0 1 * * | 1st of every month | Monthly summary |
0 0 1,15 * * | 1st and 15th monthly | Twice-monthly backup |
0 8-17 * * 1-5 | Hourly weekdays 08-17 | Business hour task |
Troubleshooting Cron Jobs
If your Cron Job is not running as expected, troubleshoot in this order:
1. Check Full Paths
Cron runs in a minimal environment where $PATH is not set the same as your login shell. Always use absolute paths:
# â May fail
php /home/user/backup.php
# â
Correct
/usr/local/bin/php /home/user/public_html/backup.php
2. Check File Permissions
chmod 755 /home/USERNAME/public_html/cron/backup.php
Scripts must be executable by the file owner.
3. Check Email Log from Cron
By default, Cron emails any output to the DirectAdmin account email. Check your inbox for error messages when a job runs.
4. Test Script Manually First
/usr/local/bin/php /home/USERNAME/public_html/cron/backup.php
Run the script directly in SSH to confirm it works before scheduling.
5. Verify Server Timezone
Best Practices and Caveats
Don't Schedule Too Frequently
A Cron Job that runs every minute and takes 5 minutes to complete will spawn multiple concurrent instances, consuming hosting resources and potentially slowing your website. Only schedule as frequently as your task genuinely requires.
Use Output Redirection
# Keep a log file
0 2 * * * /usr/local/bin/php /home/USER/backup.php >> /home/USER/cron.log 2>&1
# Suppress all output
0 2 * * * /usr/local/bin/php /home/USER/backup.php > /dev/null 2>&1
Test Before Deploying
Always run your script manually first to confirm it completes without errors before adding it as a Cron Job.
Check Resource Limits
Some hosting plans limit the number of Cron Jobs or CPU time per job. Avoid running heavy tasks during peak traffic hours to maintain website performance for visitors.
define('DISABLE_WP_CRON', true); to wp-config.php, then set up a real server Cron Job calling wp-cron.php. This is more reliable and doesn't consume PHP resources on every page request.
Frequently Asked Questions
What is a Cron Job?
A Cron Job is a scheduled task on a Linux server that runs automatically at specified intervals, managed by the cron daemon using a crontab configuration file.
Does DirectAdmin support Cron Jobs?
Yes. DirectAdmin has a built-in Cron Jobs manager under Advanced Features. You can add and manage scheduled tasks without editing crontab directly.
What do the 5 fields in a cron expression mean?
The 5 fields represent: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-7). For example, 0 2 * * * means every day at 02:00.
Why is my Cron Job not running?
Common causes are incorrect file paths, missing execute permissions, wrong PHP version path, or syntax errors. Check email notifications from cron and test your script manually first.
Can I run a PHP script with Cron?
Yes. Use /usr/local/bin/php /home/USERNAME/public_html/script.php. Always use the full path for both the PHP binary and your script file.
How often can I run a Cron Job on shared hosting?
Most plans allow every-minute scheduling, but running resource-intensive tasks too frequently can impact server performance. Use the minimum frequency needed for your actual use case.
Want hosting with full Cron Job support via DirectAdmin? View AsiaGB Hosting Plans â