Cron Jobs
Cron jobs let you schedule scripts and commands to run automatically at specified times or intervals. They are useful for tasks like sending emails, cleaning up temporary files, or running maintenance scripts.
What Is a Cron Job?
A cron job is a scheduled task that the server executes automatically. You define when the task runs (e.g., every hour, once a day, every Monday) and what command to execute. The server handles the rest without any manual intervention.
Accessing Cron Jobs
- Log into cPanel.
- In the Advanced section, click Cron Jobs.
Setting Up the Cron Email
By default, cron sends the output of each job to an email address. At the top of the Cron Jobs page:
- Enter an email address in the Cron Email field to receive job output.
- Click Update Email.
>/dev/null 2>&1 to the end of the command.
Adding a Cron Job
- On the Cron Jobs page, scroll to Add New Cron Job.
- Select a schedule from the Common Settings dropdown for presets like "Once Per Hour" or "Once Per Day", or set a custom schedule using the five time fields:
- Minute — 0-59
- Hour — 0-23
- Day — 1-31
- Month — 1-12
- Weekday — 0-6 (Sunday = 0)
- Enter the command to run in the Command field.
- Click Add New Cron Job.
Common Cron Job Commands
Running a PHP Script
To execute a PHP script on a schedule:
/usr/local/bin/php /home/username/public_html/cron-script.php
Replace username with your cPanel username and adjust the file path as needed.
Fetching a URL
To trigger a script by requesting its URL:
/usr/bin/curl -s https://yourdomain.com/cron.php >/dev/null 2>&1
Or using wget:
/usr/bin/wget -q -O /dev/null https://yourdomain.com/cron.php
Running a Shell Script
/bin/bash /home/username/scripts/cleanup.sh
/usr/local/bin/php, /usr/bin/curl). You can find the correct path by running which php or which curl in the cPanel Terminal.
Schedule Examples
Here are some common scheduling patterns:
- Every 5 minutes — Minute:
*/5, all others:* - Every hour — Minute:
0, all others:* - Every day at midnight — Minute:
0, Hour:0, all others:* - Every Monday at 3:30 AM — Minute:
30, Hour:3, Day:*, Month:*, Weekday:1 - First day of every month — Minute:
0, Hour:0, Day:1, Month:*, Weekday:*
Managing Cron Jobs
Existing cron jobs are listed at the bottom of the Cron Jobs page. For each job you can:
- Edit — Click the pencil icon to change the schedule or command.
- Delete — Click the X icon to remove the cron job.
Troubleshooting
If your cron job is not running as expected:
- Check the command path — Make sure you are using the full path to the interpreter and the script file.
- Check permissions — Ensure the script file is readable and executable (e.g.,
chmod 755 script.shfor shell scripts). - Review cron email output — Remove
>/dev/null 2>&1temporarily to see error messages sent to your cron email. - Test the command manually — Run the exact command in cPanel's Terminal to check for errors.