Cron Jobs
Automate repetitive server tasks using cron jobs. Learn the syntax, see examples, and set up your own.
Overview
A cron job is a scheduled task that runs automatically on the server at a time you specify. Think of it like an alarm clock for your server — you set the time and what should happen, and the server handles the rest. Cron jobs are perfect for repetitive maintenance tasks that would be tedious or impractical to do by hand.
Here are some real-world examples of what cron jobs can do:
- Daily backup scripts — Automatically create a backup of your website files and database every night at 2 AM.
- Scheduled email reports — Send a weekly summary email to your subscribers every Monday morning.
- Database cleanup — Delete old records, session data, or spam comments from your database every hour.
- SSL renewal checks — Run a script weekly to check if your SSL certificate is about to expire and renew it.
- Cache clearing — Clear your website cache every few hours to keep your site running fresh.
Cron Syntax
Every cron job has a schedule written in a special format with five time fields, followed by the command to run:
| Field | Allowed Values | Description |
|---|---|---|
| Minute | 0 - 59 | The minute the command runs |
| Hour | 0 - 23 | The hour of the day (0 = midnight) |
| Day of Month | 1 - 31 | The day of the month |
| Month | 1 - 12 | The month of the year |
| Weekday | 0 - 7 (0 and 7 = Sunday) | The day of the week |
An asterisk * means "every possible value" for that field. For example, * * * * * runs the command every single minute. Commas separate multiple values (1,15 means minutes 1 and 15), and hyphens define ranges (1-5 means Monday through Friday).
Common Cron Job Examples
| Schedule | Meaning |
|---|---|
0 2 * * * |
Daily at 2:00 AM — perfect for overnight backups |
0 * * * * |
Every hour at the start of the hour — good for frequent checks |
30 8 * * 1 |
Every Monday at 8:30 AM — weekly reports |
*/15 * * * * |
Every 15 minutes — useful for high-frequency tasks |
0 0 1 * * |
First day of every month at midnight — monthly cleanup |
0 3 * * 0 |
Every Sunday at 3:00 AM — weekly maintenance |
How To Create a Cron Job
Adding a Cron Job in Havenoro CP
- Log in to your control panel and navigate to the Cron Jobs section (often under Advanced settings).
- Click Add Cron Job or Create.
- Enter the minute, hour, day, month, and weekday values for your schedule.
- In the Command field, enter the full command you want to run. For example:
php /home/username/web/domain.com/public_html/cron.php - Optionally, enter an email address to receive the output of the cron job (useful for debugging).
- Click Save or Add. The cron job is now active and will run on schedule.
Viewing Cron Job Logs
If a cron job does not seem to be working, check the logs. In Havenoro CP, you can usually view the last run time and output for each cron job. Look for a Log or History button next to each cron job entry. The logs show whether the command ran successfully and any error messages.
Important Tips
- Test commands manually first. Run the command in a terminal or via the File Manager to make sure it works before adding it as a cron job. Debugging a cron job is harder than debugging a command you run directly.
- Be careful with frequent jobs. A cron job that runs every minute might seem harmless, but if it runs a resource-intensive script, it can slow down your server. For most tasks, daily or hourly is sufficient.
- Use full paths. Cron jobs run in a minimal environment. Always use the full path to your script and any files it references. For example, use
/usr/bin/phpinstead of justphp. - Watch your disk space. If a cron job generates log files or backups, make sure it also cleans up old files so you do not run out of disk space.
Next Steps
Cron jobs are great for automation. Now learn about storing structured data by heading to the Databases guide.