What is a cron job?
A cron job is a task the server runs on a schedule. It is like an alarm clock that starts a program. PHP is a language many websites use. You can use cron to send emails, clean old data or make reports.
Before you start
- Upload your PHP script to your hosting account. Write down its full path, such as
/home/username/public_html/task.php. - Test the script by hand first. A broken script will fail every time it runs.
- Find Cron Jobs in your hosting control panel. Ask Hostvento support if you cannot see it.
Understand the time fields
A schedule has five fields. They are minute, hour, day of month, month and day of week.
| Schedule | Meaning |
|---|---|
* * * * * | Every minute |
*/5 * * * * | Every 5 minutes |
0 * * * * | Every hour |
0 2 * * * | Every day at 2:00 AM |
0 0 * * 0 | Every Sunday at midnight |
An asterisk means "every". Server time may differ from your local time.
Steps in the control panel
- Log in to your hosting control panel.
- Open Cron Jobs.
- Pick a ready-made time from the drop-down, or fill in the five time boxes.
- In the Command box, type the command below. Use your own path.
- Click Add New Cron Job.
/usr/local/bin/php /home/username/public_html/task.php
This runs the script with PHP. The location of PHP can differ. Your control panel or Hostvento support can tell you the right path. You can find it on a server by typing which php.
Run it from the command line instead
- Log in over SSH.
- Type
crontab -eto edit your schedule. - Add a line with the time and command.
- Save and close the file.
0 2 * * * /usr/bin/php /home/username/task.php >> /home/username/cron.log 2>&1
This runs every day at 2 AM. The end part saves any output and errors into a log file, which helps you fix problems.
Common problems
- Nothing happens: Check the path to PHP and to your script.
- Too many emails: Cron emails you the output. Add
> /dev/null 2>&1at the end of the command to silence it. - Runs too often: Check the time fields.
Quick recap
- Cron runs tasks on a schedule.
- Use the full path to PHP and to your script.
- Set the five time fields carefully.
- Save output to a log to catch errors.