Running a PHP file with a cron job

PHP/MySQL | Updated 2026

You can schedule PHP scripts to run automatically at set intervals using cron jobs. This is useful for tasks like database maintenance, sending email digests, importing data feeds, or running cleanup scripts.

01. Setting Up the Cron Job

  1. Log into cPanel > Advanced > Cron Jobs
  2. Set the schedule using the dropdown menus (minute, hour, day, month, weekday)
  3. Enter the command:
/usr/local/bin/php /home/username/public_html/script.php >/dev/null 2>&1

The >/dev/null 2>&1 part suppresses output so you do not receive an email every time the cron runs. Remove it if you want email notifications of the script's output.

For a complete guide to cron jobs including schedule examples, see How to Create a Cron Job in cPanel.

Finding the PHP Path

The default PHP CLI binary is at /usr/local/bin/php. If you need a specific PHP version, use the versioned path like /usr/local/bin/ea-php81 for PHP 8.1 or /usr/local/bin/ea-php82 for PHP 8.2. Check Server Paths for all available paths.

Common Mistake

Do not use a relative path like php script.php. Cron jobs do not run from your web directory, so relative paths will fail. Always use the full absolute path to both the PHP binary and your script file.

02. Getting Emails for Every Cron Run?

If you are receiving emails every time the cron runs and want to stop them, add >/dev/null 2>&1 to the end of the command. See Why Do I Get Emails for Cron Jobs? for more detail.

For automated database backups via cron, see How to Backup a MySQL Database.

Cron Job Not Running?

If your cron job is not executing, check the command path and permissions. Open a ticket if you need help debugging.

Open a Support Ticket

Quick Recap

  1. Use full paths - /usr/local/bin/php /home/user/public_html/script.php
  2. Set schedule in cPanel > Advanced > Cron Jobs
  3. Suppress output with >/dev/null 2>&1
  4. Use versioned PHP paths if you need a specific PHP version
  5. Never use relative paths in cron commands

Automating tasks with cron · Last updated March 2026 · Browse all PHP/MySQL articles

  • 290 Users Found This Useful

Was this answer helpful?

Related Articles

Learn About MariaDB

PHP/MySQL | Updated 2026 Ultra Web Hosting uses MariaDB as the database server on all shared...

PHP open_basedir Restriction in Effect

PHP/MariaDB/MySQL | Updated March 2026 The open_basedir restriction in effect warning is a...

How to Parse HTML as PHP

PHP/MariaDB/MySQL | Updated March 2026 By default, Apache only processes files with a .php...

InnoDB Error: Attempted to Open a Previously Opened Tablespace

Server Maintenance | Updated March 2026 The "Attempted to open a previously opened...

Changing Maximum Upload Size

PHP/MySQL | Updated 2026 If you are getting errors like "The uploaded file exceeds the...



Save 30% on web hosting - Use coupon code Hosting30