Today here at Arzhost, we discuss how to setup a Cron Job. Linux developers need to perform many tasks regularly or on a repeated basis that requires a bunch of time. In this scenario, they need to automate the tasks on a particular date, day, or time to avoid missing any task and save time.
A cron job is the best way to automate repeated tasks as per needs and requirements. Developers widely use the cron tool for creating a cron job.
Usually, newbies want to automate their tasks, but they don’t know how to run the cron job. That’s why we will discuss the basics of a cron job and how to create a cron job in this article. Stay with us to understand the entire process simply and easily.
What is a Cron Job?
Cron is a Linux-based utility used for the scheduling of various tasks at a particular time or date. Usually, Linux developers use this tool widely for the execution of repeated tasks. For example, you make a rule to make a backup of your server every Monday morning. In that case, you can set a cron job instead of doing it manually every time.
You can use crop jobs to modify the files on the database or perform regular tasks like sending notifications or emails regularly.
It is easy to set up a cron job and if you want to know more about the setup process, then continue reading.
Components Parts of a Cron Job
Usually, any cron job comprises of three components:
A script that you have to run
A command that is required to run the script regularly
The output or action of the script based on the command of the script. Sometimes, most people don’t know how to setup a cron job Linux for running a script, provide you some specific setup instructions. That’s why, if you don’t know about it, then contact the author of the script before setting up the cron job.
How to Understand Cron Syntax?
A syntax is a group of instructions or rules that define various symbols in a system’s programming language to understand the command. That’s why, like other programming languages, cron job also uses syntax.
Understanding the syntax for a cron job makes it easier to set up the cron job. The following is the format of a cron syntax that you can use for a cron job:
A B C D E USERNAME /path/to/command arg1 arg2
In the above cron syntax:
A indicates minutes range from 0 to 59
B indicates hours range from 0 to 23
C indicates days range from 1 to 31
D indicates month range from 1 to 12 (Jan to Dec)
E indicates the day of a week from 1 to 6 (Sunday to Saturday)
Username indicates the account from where you do log in
/path/to/command refers to the script or command that you want to run
Also, you have to use some operator symbols that let you use multiple values in a single field.
These symbols are:
Asterisk (*) identifies all possible values for a field
The comma (,) identifies a list of values
Dash (-) is used for a range of values
Separator (/) identifies a step value
How to Create a Cron Job?
You can use either method mentioned below to create a cron job, i.e., set up a cron job using cPanel or set a cron job using Plesk. Let’s have a look at both methods one by one.
Setup a Cron Job Using cPanel
Follow the below-mentioned steps for creating the cron job in cPanel:
Login to cPanel at ARZHost
Click on a cron job in the advanced menu or search for a cron job
Update the email address for receiving the updates about the cron job
As you click on the cron job in the advanced menu, you can see the option “add a new cron job,” just click on it.
Provide the command either directly or by uploading the file containing commands.
Click on next and select the frequency of command in the command settings from the options available in the form of a drop-down list. You can also set the frequency by providing manual settings of time and date.
After finishing the setup, just hit the add new cron job button, and you are done with the process.
It is a simple process. You can create multiple cron jobs by repeating the entire process and providing different commands.
Setup a Cron Job Using Plesk
Plesk is an alternative for creating the cron job on Linux and windows based servers. Follow the following steps for understanding the process of creating a cron job using Plesk.
Login Plesk control panel at ARZHost
1: Click on the tools and settings option in server management that you can see on the left side of the dashboard
2: Click on scheduled tasks under the tab tools & resources and then click on Plesk administrator user
3: Click on add new tasks on the screen of scheduled tasks and provide the required information, including command line or path to the command file
Select the frequency of task by providing information about time, date, and month by using calendar under the section of task priority
After selecting the frequency of tasks, just hit OK to complete the process, and you are done with the process
Setting Up the Cron Job in WordPress
As we know, WordPress is the largest platform for building websites. That’s why it uses its built-in features for regulating different tasks like sending emails, notifications, taking updates, deleting junk messages, and many others. But some people like to outsource the cron job feature instead of using a built-in feature. It is due to a lack of reliability, in their opinion.
Although you can also create a cron job on the WordPress site using cPanel, we will let you know about creating the cron job using the WordPress plugin.
Follow the below-mentioned steps for understanding the entire process easily:
Step # 1,
Login to your WordPress dashboard and go to the plugins area. You can see the “add new plugin” button, click on it, and search for WP Control Plugin.
Download and install the plugin.
Step # 2,
After downloading the WP Control Plugin, you can see the option of events under the tools tab in the WordPress dashboard menu on the left. Here you can see the list of cron jobs, but you can see it blank if you had never created the cron job before.
Click on the add cron event tab next to the cron event tab. Here you can see different fields like name of cron event, frequency, and date, etc. Provide the information as per your needs and hit the add cron event button.
Step # 3,
As you complete adding a new cron event, you can see a newly created event in the list under the cron events tab. But you can also see the error “None” under the action column. It means you don’t set the action for this cron event.
You can add the following coding in the PHP file of the theme just under “ function cw_function() {
wp_mail( ‘info@example.com’, ‘new cron’,!’ );
}
You can access the PHP file of the theme in the dashboard of WordPress and edit it accordingly. After adding the file, hit the save button, and you are done with the process.
Conclusion
It is easy to create a cron job for performing repeating tasks, but it requires little attention. It helps save time and helps in completing important tasks on time, like backup or sending notifications. If you follow the instructions provided above, you can easily do it.
Hopefully, all the discussions in the above article help you understand the process of creating a cron job.