Introduction to Cron Command
The following article provides an outline for Cron Command. Cron commands will help us run a particular task and schedule the same automatically after a certain time period. The user can schedule a particular repetitive task and are to be executed after a certain time interval can be scheduled by using cron commands that are responsible for executing the tasks written in scripts in any programming language. Cron commands have vast application in the daily life scenarios.
Overview of Cron Command
- In cron, a daemon thread runs in the background to keep a complete check on which cron commands or tasks need to be executed at what time. Thus, we can prepare a list of commands that needs to be executed periodically, such as taking the backups of a certain server, updating a particular page, or synchronize two tasks after a particular time interval.
- It keeps an eye on each and every scheduled task, and when the period or time mentioned while creating a cron task is the same as that of the current time, it goes for executing those particular commands. You don’t have to do any extra efforts to start the cron as it automatically starts whenever you will enter a run level that are multi-user based.
- The start of cron happens from the folder directory that’s usually etc/init.d.
How to Use Cron?
After you have added the cron job, it’s 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. There are many editors and tools which provide the interface for using a cron job in PHP. One of them is Cpanel.
The steps involved for creating a cron job using Cpanel for PHP program are as listed 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 button named Add New Cron job and enter the details mentioned in 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 you want to execute situated in the php script path by mentioning the same in the command text box.
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.
- In case if you want the settings of the cron job to be done manually, then you can go for clicking on the Advanced option in the menu section.
Crontab File Contents on Server
The contents of the crontab file on the server can be a file in a particular programming language such as PHP or a shell file with a .sh extension containing the commands to be executed under the scheduled cron job.
For example, we might have a file with the name educbaCron.php that contains the following task of echoing a message.
<?php
echo "You can come and visit EDUCBA to extend your knowledge in different domains.";
?>
There is a separate crontab file that you can edit and list by using a crontab command with different options as specified here.
Crontab [-l | -e] username
-l and -e are the options which stand for editing and listing respectively while username is the name of the user using which you are performing operations on the crontab file.
Examples of Cron Command
Given below are the examples of Cron Command:
In order to allow a particular user to access and manipulate the cron jobs, you can make the use of the following command.
Code:
/etc/cron.allow
Output:
If you want a particular user to restrict the access to cron jobs, then simply execute the below deny command.
Code:
/etc/cron.deny
Output:
Suppose that you want to create a cron job by using a command-line interface such as a terminal instead of using any other tool such as Cpanel. You have one task of running a particular script file located in a specific directory, say for example /D/Users/Payal/cronExamples/ EDUCBA/giveMessage.sh for a time duration of every hour starting from 10:00 AM to 3:00 PM for all days then your command for creating cron job for it will be as shown below.
Code:
00 10-15 * * * /D/Users/Payal/cronExamples/ EDUCBA/giveMessage.sh
Output:
To check the complete list of all the cron jobs schedule, you can fire the following command.
Code:
Cronitor list
Output:
Conclusion
Cron command includes creating a cron job or listing the existing created cron jobs that are scheduled for execution by using the command-line interface.
Recommended Articles
We hope that this EDUCBA information on “Cron Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.