The cron can be defined as a software utility that is provided by a Linux-like OS that operates the scheduled operation on a predetermined period. It's a daemon process and executes as a background process. It performs the described tasks at a predefined period when a condition or event is encountered without user intervention.
Frequently, dealing with any repeated operation is an intimidating operation for a system administrator. Hence, the system administrator can schedule these types of processes for running in the background automatically at regular time intervals by making a list of the commands with the help of the cron command.
It enables all the users to run the scheduled operation on the regular basis like doing a backup every single day, synchronizing files on a few regular intervals, and scheduling updates on a weekly basis.
Recurrently, Cron inspects the scheduled task and when the fields of scheduled time match the fields of the current time, the scheduled commands will be executed. Automatically, it is begun from the /etc/init.d file on entering run levels of multi-user.
Basis Syntax of Crontab
Cron reads all the configuration files for the command's list to run. The daemon applies a particular syntax for interpreting the lines inside the configuration tables of crontab.
The crontab ("cron table") is a list of commands for executing the scheduled operations at a particular time. It permits the user for adding, removing, or modifying the scheduled operations.
The syntax of the crontab command contains six parts separated by a space in which the first five parts illustrate the time to execute the operation and the last part illustrates the command.
Minute (it holds the values from 0 to 59).
Hour (it holds the values from 0 to 23).
Day of Month (it holds the values from 1 to 31).
The month of the year (it holds the values from Jan to Dec or 1 to 12, the initial three letters of the name of the month shall be applied).
Day of the week (it holds the values from Sun to Sat or 0 to 6, the initial three letters of the day shall be applied).
Options in Cron Command
1. -f: This option is used for staying in foreground mode and does not daemonize.
2. -1: This option will enable the names of the LSB compliant for a file, i.e., /etc/cron.d.
3. -n: This option is used for adding the FQDNs within the subject when transferring mails.
4. -L loglevel: It will inform the cron that what to log of the tasks using some values which are mentioned as follows:
1. This value will log the beginning of each cron task.
2. This value will log the completion of each cron task.
4. This value will log every failed task and the status of exit will not equivalent to zero.
8. This value will log the number of the process of each cron task.
The following are some rules which define the date and time format field:
Allowing users to execute the cron tasks:
The user should be listed inside this file to be capable to execute the cron tasks when the file is available:
In case, the file, i.e., /etc/cron.allow does not exist but the file, i.e., cron.deny exists, a user should not be listed inside this file to be capable to execute the cron task.
Important: In case. Neither of the above files is available then only the system administrator (superuser) will be permitted for using a provided command.
Command to Run
It defines the command to run. It illustrates the exact filename or directory of the commands or scripts we wish cron to execute.
An example is mentioned below:
The above command looks over the system's root directory and executes the script, i.e., backup.sh. We may define any command or script we want.
cron by default transfers an email to the crontab file's owner when it executes. It is an efficient way for keeping a record of operations. Remember that minor or regular operations can quickly fill up our inbox.
We can prevent the scenario by stopping an output email because it is an optional aspect.
For turning off the email output, we can include the string, i.e., >/dev/null 2>&1 after the command and timing fields.
Setting up the Cron Operation
We need to open the crontab along with a text editor for configuring the cron operation and enter the syntax for a command we wish to execute.
Editing a crontab file
We need to type a command in our terminal window for opening the configuration file of the crontab for a current user. The command is mentioned as follows:
We can include any scheduled operation one per line.
Save that file and then exit once we have finished including operations. The daemon of cron will read and run the instructions given.
Note: Cron doesn't require to be rebooted to apply modifications.
Editing crontab for a Distinct User
We can enter a command for editing a crontab for a distinct user. The command is mentioned as follows:
Examples of Cron Operation
We can use the symbol for specifying every value when describing the operations. Entering a value inside any field only executes the command over that value.
It only executes if the first of a month (0) will fall over a Wednesday (4) even though it is set to execute at 2 am. If we modify to the below:
The above command will execute the first of all months at 2 am. The table facilitates some common commands with a file, i.e., /root/backup.sh through our previous example. This table is mentioned below:
Some points must be remembered at the time of working using cron. These points are mentioned below: