- How to set the cron job to run a PHP script in cPanel?
- What is Cron?
- What is PHP Script?
- How to set the cron job to run a PHP script in cPanel?
- Set a cron email:
- Add New Cron Job:
- Edit a cron job:
- Delete a cron job:
- Php script cron cpanel
- Table of Contents
- Cron Jobs
- Version:
- 82
- Overview
- Add a cron email
- Disable email notifications
- Add a cron job
- Cron job example
- View existing cron jobs
- Edit a cron job
- Delete a cron job
- Additional Documentation
- Set the cron job to run a PHP script in cPanel
- What is a cron job?
- Steps to set the cron job to run a PHP script in cPanel
- 1. Set a cron job in cPanel
- 2. Edit a cron job
- 3. Set an email for notifications in cron job
- How to create a cronjob to run a PHP script (cPanel)
- Login to cPanel
- Creating a cron to run PHP
How to set the cron job to run a PHP script in cPanel?
This article will help you set up the 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 about running 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.
- Timing – set the minutes, hours, days, months, and weekdays.
- Execute – the cron job needs to call PHP to run, which is located at /usr/bin/php path.
- Script Path – the full path of the file you want to run.
[For example, /home/user/public_html/index.php] - 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. Log in to cPanel.
2. Go to cPanel >> Home >> Advanced >> Cron Jobs.
Set a cron email:
In the Email text box, enter the email address on which you wish to receive the cron notifications. Click on the 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, 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 the 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 you wish to run the cron job.
2. Click Add New Cron Job button.
Edit a cron job:
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:
To delete a cron job, perform the following steps:
— Click on the Delete button.
NOTE: If you schedule a cron job to run very often, it 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.
Php script cron cpanel
Did you find this document helpful?
Would you like to provide more feedback on this document?
Table of Contents
Cron Jobs
Valid for versions 82 through the latest version
Version:
82
Last modified: November 29, 2022
Overview
Cron jobs are scheduled tasks that the system runs at predefined times or intervals. Typically, a cron job contains a series of simple tasks that the system runs from a script file.
- Exercise caution when you schedule cron jobs. We recommend that you allow enough time between cron jobs for the previous cron job to complete. If you schedule them to run too often, the server could start another cron job before the last cron job ends. This duplication may degrade performance.
- For more information about cron jobs, read Red Hat’s Scheduling a Cron Job documentation.
Add a cron email
The Cron Email section of the interface allows you to enter an email address for the system to send notifications when your cron jobs run. To set an email address, perform the following steps:
- In the Email text box, enter the email address at which you wish to receive the notifications.
- Click Update Email.
Disable email notifications
To disable email notifications for all cron jobs, remove the email address.
To disable email notifications for a single cron job, perform the following steps:
- Locate the cron job for which to disable email notifications in the Current Cron Jobs table and click Edit.
- In the Command text box, add the /dev/null 2>&1 line to the end of the command. For example:
/usr/local/cpanel/bin/is_script_stuck /dev/null 2>&1
Add a cron job
To create a cron job, perform the following steps:
- Select the interval at which you wish to run the cron job from the appropriate menus, or enter the values in the text boxes.
- Common Settings — Select a commonly-used interval. The system will configure the appropriate settings in the Minute, Hour, Day, Month, and Weekday text boxes for you.
- Minute — 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 — 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 — 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 — 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 — The days of the week on which you wish to run the cron job.
- In the Command text box, enter the command that you wish the system to run.
- You must specify settings for the Minute, Hour, Day, Month, Weekday, and Command text boxes.
- Exercise extreme caution when you use the rm command in a cron job. If you do not declare the correct options, you may delete your home directory’s data.
- If your cron job runs a custom script, the script requires the execute permission. For more information, read Red Hat’s Linux Permissions documentation.
Specify the absolute path to the command that you wish to run. For example, if you wish to run the public_html/index.php file in your home directory, enter the following command:
/home/user/public_html/index.php
Cron job example
To run a script that resides in your home directory once an hour, perform the following steps:
- In the Common Settings menu, select the Once Per Hour(0 * * * *) option.
- In the Command text box, enter the script’s name. For example:
View existing cron jobs
The Current Cron Jobs table displays your existing cron jobs.
Edit a cron job
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
To delete a cron job, perform the following steps:
Additional Documentation
Set the cron job to run a PHP script in cPanel
Today we will explain how to set cron jobs and run php scripts using cron jobs. Let you explain what is cron before we start.
What is a cron job?
A cron job is a Linux command which schedules a script on your server to run automatically at defined times or regular intervals. Cron is used to ensure timely execution of scripts and also used for deleting temporary files to conserve your disk space, for backups.
How to run PHP scripts from cron jobs, Execute PHP script in cron job , how to run php file using cron jobs, How to run a PHP script using a Cron job, Run a PHP file in a cron job using CPanel, how to run a php script daily with the cron job, How to set the cron job to run a PHP script in cPanel, Run cron job on cpanel with php7 version, Cron Job, Run PHP script, php cron scheduler, on linux centos how set cron job for php file.
Checkout more articles on PHP
Steps to set the cron job to run a PHP script in cPanel
1. Set a cron job in cPanel
The following steps are used to set the cron job in cPanel.
Step 1: Login to cPanel
Login to cPanel and go to ADVANCED > Cron Jobs.
Step 2: Cron job settings
In the Add New Cron Job section, you can select common settings or add custom values. Here we set a cron job twice per hour.
If you want to set custom time then follow the below cron job format.
- Minute (0 to 59)
- Hour ( 0 to 23)
- Day of month (1 to 31)
- Month (1 to 12)
- Day of week (0 to 6 are Sunday to Saturday)
Step 3: Add command to set a cron job
In the Command field, if you want to run PHP Script file named script.php that located in the public_html directory then command will be
After adding the command in the Command textbox, click on the Add New Cron Job button to set your cron job and it will appears in the list as shown below.
2. Edit a cron job
To edit a cron job, follow as below.
- Click on the Edit button that is located in the cron job list to edit the cron job.
- Edit the settings that you want to make and click on the Edit Line button.
3. Set an email for notifications in cron job
Also we can notify via email when a cron job runs, just we need to add an email address in the Cron Email section.
If more than one cron job is set and you do not want email notifications for a particular cron job then you can add >/dev/null 2>&1 after command like below.
How to create a cronjob to run a PHP script (cPanel)
In this article we will learn how to create a cronjob to run a PHP script in cPanel. We have recently updated cPanel to the Jupiter theme, the latest one, so do not be worried if our interface looks different from your cPanel theme.
Login to cPanel
Just in case you do not know where cPanel is located, you can get there in two ways.
- cPanel is generally located on port 2083, so you can access the login with the IP and port, for example: https:// 192.168.0.1 : 2083 .
- Another option is to access cPanel from the WHM server administration panel, like it’s show in the image below.
Creating a cron to run PHP
Once you are in cPanel, navigate to the Advanced section and find Cron Jobs icon.
Cron job is a handy tool which allows to automate certain tasks on the server. For example, your site may need to run certain script once a day to create a backup or send notifications to customers etc. With the cron job you can run a certain script in defined times with a set frequency.
If you want an email to be sent every time cron job is run, you can set one in the Cron Email like on the image above. But generally it’s not very useful, because most times you will know by itself if the script runs ok or something needs to be fixed.
cPanel now has a nice intuitive interface for cron jobs creation. You can set your own intervals for the cron job or you can select one of the most commonly used within Common Settings.
Here we have chosen the Twice Per Day in the common settings and all other fields were filled automatically.
Now we only need to specify the command and add the cron job. For this task we will need two things:
- Location of the script: we will need full path on the server. On cPanel it generally looks like, /home/username/public_html/location/of/the/file.php
- Command to run a PHP script. For cPanel, it will be: /usr/local/bin/ea-php74. You need to replace ea-php75 with your PHP version on the server, it can be ea-php76 or ea-php80 or some other.
We only need to press “Add New Cron Job” and it will appear in the list of “Current Cron Jobs“.
This is it on how to create a cron job and schedule to run a PHP script with its help. You can edit or remove newly created crons any time.