How to set up a cron job
What is a cron job?
A cron job runs a command automatically on a schedule.
Common uses include:
- Running application maintenance
- Sending queued messages
- Processing scheduled tasks
- Creating application-generated reports
- Running Laravel's scheduler
- Calling a maintenance script
Do not schedule a job more frequently than the application requires.
Understand the schedule
A cron schedule contains five time fields:
minute hour day-of-month month day-of-week
Examples:
| Schedule | Meaning |
|---|---|
0 * * * * |
At the start of every hour |
*/15 * * * * |
Every 15 minutes |
30 2 * * * |
Every day at 02:30 |
0 4 * * 1 |
Every Monday at 04:00 |
0 0 1 * * |
At midnight on the first day of each month |
Times are South African time (SAST).
Create a cron job in cPanel
- Sign in to cPanel.
- Open Advanced.
- Select Cron Jobs.
- Choose a common schedule or enter the time fields.
- Enter the command.
- Select Add New Cron Job.
Create a cron job in DirectAdmin
- Sign in to DirectAdmin.
- Open Advanced Features.
- Select Cron Jobs.
- Select Create Cron Job.
- Enter the schedule and command.
- Save the cron job.
Use absolute paths
Cron does not necessarily start in your website folder. Use full paths to scripts and files.
Example:
/usr/local/bin/php /home/USERNAME/public_html/path/to/script.php
For DirectAdmin:
/usr/local/bin/php /home/USERNAME/domains/DOMAIN/public_html/path/to/script.php
Replace the placeholders with your account details.
/usr/local/bin/php runs the PHP version you selected for your account.
Save output to a log
To save both normal output and errors:
/path/to/command >> /home/USERNAME/cron.log 2>&1
Make sure the destination folder exists and is writable.
Review or remove old cron logs periodically so they do not consume unnecessary disk space.
Avoid exposing secrets
Do not place passwords or API keys directly in a cron command where possible.
Use:
- Environment variables
- A protected configuration file outside
public_html - The application's normal configuration system
Test the command first
- Add the job with a schedule a few minutes ahead, and send its output to a log as above.
- Check the log after it runs.
- Fix any errors, then set the real schedule.
SSH is switched off by default on shared hosting. If you want to run commands by hand first, open a support ticket to ask for SSH access.
Did this answer it?