What is a Cron Job? How to Set Up Cron on Thai Hosting 2026
A Cron Job is a scheduled task that runs automatically at specified intervals on your hosting server. The name "cron" comes from the Greek word for time (chronos). Cron jobs are essential for automating repetitive tasks like database backups, email newsletters, sitemap updates, cache clearing, and log rotation — without any manual intervention.
Cron Expression Syntax
A cron expression consists of 5 time fields followed by the command to execute:
* * * * * command
| | | | |
| | | | └── Day of week (0=Sun, 1=Mon...6=Sat)
| | | └──── Month (1-12)
| | └────── Day of month (1-31)
| └──────── Hour (0-23)
└────────── Minute (0-59)
Use * for "any value", numbers for specific values, */n for "every n units", and commas for multiple values.
Common Cron Expression Examples
0 * * * *— Run every hour, at minute 00 2 * * *— Run daily at 2:00 AM0 2 * * 0— Run every Sunday at 2:00 AM0 2 1 * *— Run at 2:00 AM on the 1st of each month*/5 * * * *— Run every 5 minutes0 9,17 * * 1-5— Run at 9AM and 5PM, Monday through Friday
Creating Cron Jobs in DirectAdmin
- Login to DirectAdmin → Advanced Features → Cron Jobs.
- Fill in the Minute, Hour, Day, Month, Weekday fields and the Command.
- Example — daily database backup at 2AM:
/usr/bin/mysqldump -u dbuser -p'dbpass' dbname > /home/user/backup/db_$(date +\%Y\%m\%d).sql - Click Add Cron Job.
WordPress WP-Cron
WordPress has its own scheduling system called WP-Cron, which runs wp-cron.php every time a visitor loads a page. The problem: if your site has no visitors, scheduled tasks won't run.
Solution: Disable WP-Cron and create a real server cron job instead:
# In wp-config.php, add:
define('DISABLE_WP_CRON', true);
# Create a real cron job in DirectAdmin:
*/5 * * * * /usr/bin/php /home/user/public_html/wp-cron.php > /dev/null 2>&1
Popular Cron Job Use Cases
- Database backup: mysqldump every night at 2:00 AM
- Newsletter sending: Process email queue every 15 minutes
- Sitemap updates: Regenerate sitemap.xml every 6 hours
- Cache clearing: Remove expired cache files every hour
- Log rotation: Archive and delete old log files weekly
- SSL expiry check: Daily verification of certificate expiry dates
- Temp file cleanup: Remove temporary upload files daily
Troubleshooting Cron Jobs
- Wrong path: Always use absolute paths in cron (e.g., /usr/bin/php, not just php).
- Wrong permissions: Script files must have execute permission (chmod +x).
- Debug with logging: Add
>> /tmp/cron.log 2>&1to capture output for debugging. - Environment differences: Cron runs in a minimal environment without the same PATH as your shell session.
Frequently Asked Questions
What is the most frequent interval a cron job can run?
Every 1 minute (*/1 * * * *) is the minimum for standard cron. However, many hosting providers restrict cron to run no more frequently than every 5 minutes. Check your hosting provider's cron policy.
How do I know if a cron job failed?
Cron typically emails the account when a job fails, if MAILTO is configured. Alternatively, add error log redirection at the end of your command: 2>> /tmp/cron-error.log
What happens if a cron job runs longer than its scheduled interval?
Cron will start a new instance overlapping the still-running job, potentially causing race conditions or data corruption. Prevent this using lock files or the flock command.
What's the difference between cron jobs and WordPress Scheduled Posts?
WordPress Scheduled Posts use WP-Cron, which only runs when visitors load pages. If your site has low traffic, posts may publish late. Setting up a real server cron job to trigger WP-Cron every 5 minutes fixes this reliably.