Javatpoint Logo
Javatpoint Logo

Cron Command

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.

Prerequisites

  • A system having Linux operating system
  • Access to the terminal/command-line window (Ctrl+Alt+F2 or Ctrl+Alt+T)
  • A user account along with sudo or root privileges

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).

Command.

Syntax:

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:

  • If one of the five fields is fixed to an asterisk symbol (*). It stands for each field value. For example, we can put the asterisk symbol (*) for executing the command daily in the field of the week.
  • Also, we can apply the range of numbers that are separated by a hyphen symbol (-) in the date and time field for including multiple contiguous values but not each field value. For instance, we can apply the 7-10 for running any command from Jul-Oct.
  • The comma operator (,) is used for including the number's list which might or might not be consecutive. For instance, "1, 3, 5" in the field of week signifies the command execution every Mon, Wed, and Fri.
  • The slash (/) character is added for skipping the values of the provided numbers. For example, "*/4" in the field of hour defines "every 4 hours" and it is equal to the 0, 4, 8, 12, 16, 20 numbers.

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.

Output (Optional)

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:

Cron Command

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.

For instance:

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:

S.No. Command Cron Operation
1. * * * * * /root/backup.sh Run Cron Opeartion Every Minute
2. 30 * * * * /root/backup.sh Run Cron Opeartion Every 30 Minutes
3. 0 * * * /root/backup.sh Run Cron Opeartion Every Hour
4. 0 0 * * * /root/backup.sh Run Cron Opeartion at Midnight Every Day
5. 0 2 * * * /root/backup.sh Run Cron Opeartion Every Day at 2 am
6. 0 0 1 * * /root/backup.sh Run Cron Opeartion Every First of the Month
7. 0 0 1 5 * * /root/backup.sh Run Cron Opeartion Every Fifteen of the Month
8. 0 0 0 1 2 * /root/backup.sh Run Cron Opeartion at Midnight on December 1st
9. 0 0 * * 6 /root/backup.sh Run Cron Opeartion at Midnight on Saturday

Some points must be remembered at the time of working using cron. These points are mentioned below:

  • Have the control of source version for tracking and maintaining the modifications to the expressions of cron.
  • Organized all the scheduled operations based on their frequency or importance and group them according to their time or action range.
  • Check the scheduled operations by initially including a high frequency.
  • Don't directly write typical code or various redirections and piping in the expression of cron. Rather, write them to schedule a script and script to crontab.
  • Apply aliases if similar command sets are repeated frequently.
  • Ignore running scripts or commands using cron as a superuser.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA