How to set the cron job to run a PHP script in cPanel?

This article will help how to setup cron job for a PHP file in cPanel

What is Cron?

Cron is a Linux module that allows you to run commands at specified time intervals. It is used to schedule a task that is executed periodically. In Windows, it’s called Scheduled Tasks.

It can be used to schedule jobs such as commands or shell scripts to perform backups, schedule package updates, run script files etc., that run on the server periodically and automatically in the background at specific times, dates, or intervals.

What is PHP Script?

PHP (Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. Before you think to run a program, we will need to write a PHP script. This script could be related to the task that you want to perform.

There are four main parts of a cron command.

  1. Timing – set the minutes, hours, days, months, and weekday.
  2. Execute – the cron job needs to call PHP to run, which is located at /usr/bin/php path.
  3. Script Path – the full path of the file you want to run.
    [For example, /home/user/public_html/index.php]
  4. Output – you can write the cron output to a file or discard it, /dev/null 2>&1 will discard. You can skip this optional part.

How to set the cron job to run a PHP script in cPanel?

  1. Login to cPanel.

  2. Go to cPanel >> Home >> Advanced >> Cron Jobs.


Set a cron email:

  1. In the Email text box, enter the email address on which you wish to receive the cron notifications. Click on Update Email button.


    Disable email notifications:

    To disable email notifications for all cron jobs, remove the email address. If you do not want an Email notification for the Cron, you should add /dev/null 2>&1 in the command field. It will send the output to NULL.

Add New Cron Job:

Please refer to the following steps to add a cron job, perform the following steps.

  1. Select the interval at which you wish to run the cron job from the appropriate menus, or enter the values in the text boxes under Common Settings. It allows you to select a commonly-used interval. The system will configure the appropriate settings in the Minute, Hour, Day, Month, and Weekday text boxes for you. So, you can select the appropriate option from combo-box if you don't want to enter values manually.

  • Minute — Select the number of minutes between each time the cron job runs, or the minute of each hour on which you wish to run the cron job.

  • Hour — Select the number of hours between each time the cron job runs, or the hour of each day on which you wish to run the cron job.

  • Day — Select the number of days between each time the cron job runs, or the day of the month on which you wish to run the cron job.

  • Month — Select the number of months between each time the cron job runs, or the month of the year in which you wish to run the cron job.

  • Weekday — Select the days of the week on which you wish to run the cron job.

  • General Examples of cron job:

    * * * * * /usr/bin/php /home/user/public_html/index.php > /dev/null 2>&1

  • Domain-specific example:

    /usr/local/bin/ea-php56 /home/user/public_html/index.php

  • In the above example, replace “ea-php56” with the PHP version assigned to the domain you want to use on a script. You can go to the MultiPHP Manager to check PHP version assigned to your domain.

  • In the Command text box, enter the command that you wish the system to run OR enter the path of your PHP script.

  1. Click Add New Cron Job button.


Edit a cron job:

  1. To edit a cron job, perform the following steps:

    - Locate the cron job that you wish to edit and click Edit.

    - Edit the settings that you wish to change and click Edit Line.



Delete a cron job:

  1. To delete a cron job, perform the following steps:

    - Click on Delete button.


    NOTE: If you schedule a cron job to run very often, they may degrade your server performance.

→ Looking to set automated tasks on Linux VPS using cron? Please refer to set cron on Linux VPS for more details. 

→ Looking to set cron to backup MySQL DB and remove older backup? Please refer to set cron for MySQL DB for more details.

Related Articles

How To Automatically Delete Forwarded Mails in cPanel?

You can use cPanel email forwarder option to forward your emails to any other email account....

What is cPanel?

cPanel is a Web-based control panel provides by Hosting Providers to their clients to manage...

How to install DokuWiki using Softaculous on cPanel?

DokuWiki is a software package that allows you to create an online wiki. It is an open-source...

How to install PrestaShop in cPanel?

PrestaShop is a free and open-source e-commerce content management system (CMS). Please refer to...

How to Install IonCube in cPanel/WHM ?

IonCube loader is a free PHP extension uses to decode encrypted PHP files at runtime. Please...

  • 2 Users Found This Useful

Was this answer helpful?