Updated March 29, 2023
Introduction to Cron in PHP
The user can schedule a particular repetitive task and are to be executed after a certain time interval can be scheduled by using cron jobs that are responsible for executing the tasks written in scripts in any programming language such as PHP. Cron jobs have vast application in the daily life scenarios. Here we will see what are cron jobs in PHP, how can they be created and run along with some of the commands for cron that can be used in PHP.
What is Cron in PHP?
Cron on linux platform is the name given to scheduled tasks, while in the windows platform, it is also referred to as scheduled tasks. The derivation of the cron word comes back from chronology which stands for the order of time. In the applications written in the PHP programming language, cron jobs can be used to make sure that certain critical tasks that can be code snippets that need to execute and scheduled are executed in a timely manner.
The cron jobs have a great implementation in system maintenance as well as application level. In web applications where PHP is used widely, there occur many such situations where we need to schedule few tasks.
Cron comes up with a daemon program that is alive and executing continuously in the background, which plays the role of executing the tasks that are scheduled in cron. In addition, there is a presence of one file for configuration whose name is a crontab that is present inside the schedule and where all the information about the tasks as well as timers are present.
How to Create Cron in PHP?
While adding the cron job, you must be aware of the following parameters that play an important role in the execution of cron jobs in PHP.
- Script path: The execution task is written inside the script file whose location and path should be specified as it is needed while running the tasks.
- Execute: The cron jobs that are created should be called inside the program written in PHP as they are located inside the usr/in in the folder named php.
- Time: The period, interval, or time should be set in months, weekdays, hours, days, or minutes when you want the cron job to be executed.
- Output: We can either discard or navigate all the cron job output to a particular file located at a defined destination.
There are many editors and tools which provide the interface for adding a cron job in PHP. One of them is Cpanel.
The steps involved for creating a cron job using Cpanel for the PHP program is given below:
- Cpanel can only be accessed by firstly logging in to the platform.
- You will firstly have to open the Cpanel and then navigate to the Home option. Further, you can move to the Advanced option and then go to cron jobs. The interface will look as shown in the below image.
- Further, you can enter the email address to receive all the notifications related to cron. After that, you will have to click on the update email button. In case if you don’t want to receive any notifications from cron and disable this facility, then you can leave the field blank.
- Click on the Add New Cron job button and enter the details mentioned below of the time task and command.
- Note that you can specify your own domain in place of the usr/local/… string, and you can retrieve the version of php used by your domain. In order to go for the MultiPHP manager, you have to get to know about the version of php that is being used by your domain.
- While creating a cron job for php, you will have to create a php script that you have to run and choose the option of php shown in the below image.
- Create a php file containing the script that needs to be scheduled by using the cron job. For example, suppose that we have created the file named index.php that contains the following content in it.
<?php echo "Educba is the best edutech site present on internet"; ?>
- You can fill in the command that you want to execute situated in the php script path by mentioning the same in the command text box.
Run a Cron Job in PHP
After you have added the cron job, its very simple to run the same. You can go for executing the command line instructions or even use any tool that provides the interface for the same.
If you are using the cpanel tool, then in order to run the cron job of php you have created, you can follow the below-mentioned steps:
- After you have mentioned all the details about the execution frequency, the path to the php script that needs to be executed, and the corresponding link, you can simply click on the Apply button.
- The cron job application will take a few minutes, and after that, you will be ready to go. Then, finally, your cron job will be set and ready to run.
- If you want the cron job settings to be done manually, you can go for clicking on the Advanced option in the menu section.
Command Cron in PHP
In order to use cron job with PHP, we can manually check in the presence of the configuration property named wget that is required for cron to work with php.
The command for checking this configuration in systems based on RPM, such as Mandrake or redhat, uses the following command.
Further, you can execute the PHP file which you have created as a script, and invoking can be done by following the wget command.
#wget (URL to php file)
Cron in PHP can be used to schedule the execution of a particular tasks specified in the PHP script on the repetitive pattern as per our requirement, which can be done manually or by using any of the tools which provide such interface such as cpanel.
This is a guide to Cron in PHP. Here we discuss the introduction, how to create cron in PHP, run and command cron, respectively. You may also have a look at the following articles to learn more –