Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to run PHP scripts with cron jobs

This guide shows you how to make a PHP script run by itself at set times, such as every night.

How-To Guides2 min read9 steps

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.

ScheduleMeaning
* * * * *Every minute
*/5 * * * *Every 5 minutes
0 * * * *Every hour
0 2 * * *Every day at 2:00 AM
0 0 * * 0Every Sunday at midnight

An asterisk means "every". Server time may differ from your local time.

Steps in the control panel

  1. Log in to your hosting control panel.
  2. Open Cron Jobs.
  3. Pick a ready-made time from the drop-down, or fill in the five time boxes.
  4. In the Command box, type the command below. Use your own path.
  5. 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

  1. Log in over SSH.
  2. Type crontab -e to edit your schedule.
  3. Add a line with the time and command.
  4. 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>&1 at the end of the command to silence it.
  • Runs too often: Check the time fields.
Tip: Avoid running heavy scripts every minute. This can slow your whole site.

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.