What is a Cron Job in Linux

Cron is a daemon that executes the tasks in the system background. The tasks scheduled for later execution are known as “Cron Jobs“. Cron Jobs are considered ideal for recurring tasks such as creating system backup regularly. Linux users can automate their usual tasks hourly, daily, weekly, monthly, or yearly by using Cron Jobs. To keep track of the Cron Jobs, “Crontab files” are used, which can be user-specific or have system-wide Cron Jobs.

This write-up will guide you about Cron Jobs and its types. Moreover, we will also show you some Cron Jobs examples and the procedure to list and remove Cron Jobs from the crontab file. So, let’s start!

What is a Cron Job in Linux

Cron daemon functionality in Linux is similar to what Services do in Windows, and both are used to schedule tasks that will execute at a specific time in the system background. Cron Jobs are mainly used to regularly clean up your system cache or create a backup of the log files and databases. Whereas, the crontab file comprises all of the system Cron Jobs, and it also has a specific syntax for writing the commands.

Syntax of crontab command in Linux

The syntax to follow while writing a crontab command is given below:

* * * * * [Command or Script]

Here:

  • The first asterisk * represents “Minute
  • The second asterisk * represents “Hour
  • The third asterisk * represents “Day of Month
  • The fourth asterisk * represents “Month
  • The fifth and the last asterisk * represents “Day of Week

Now, check out the values of the fields mentioned above:

Cron Job commands in Linux

Here are some of Cron Jobs command which you can utilize in your Linux terminal:

CommandDescription
crontab -aThe “crontab -a” command is utilized for creating a user specific crontab file. You have to specify the “filename” in this command after adding the “-a” option.
crontab -rThe “crontab -r” command is utilized for deleting the crontab file.
crontab -eThe “crontab -e” command is utilized for editing the already created crontab file.
crontab -vThe “crontab -v” command is utilized to show the crontab file’s modification time.
crontab -lThe “crontab -l” command is utilized to view the crontab file’s content.

How to check Cron service in Linux

Before starting the Crob job, ensure the “cron.service” is currently active on your Linux system. For this purpose, execute the below-given “systemctl” command:

$ sudo systemctl status cron.service

The below-given output declares that cron.service is “active” and running on our system:

In case, if the “cron.service” is inactive on your Linux system, then execute the following command to start it:

$ sudo systemctl start cron.service

Types of the Cron Jobs in Linux

Cron Jobs primarily consist of two types: System-wide Cron Jobs and User-specific Cron Jobs. In the following section, we will explain the difference between both types and their related Cron Job files.

What are System-wide Cron Jobs in Linux

Cron Jobs are only accessed by the system administrator and are known as System-wide Cron Jobs. These Cron Jobs can be found in the “/etc/cron.d” directory and “/etc/crontab” file. These crontab files are executed through “/etc/cron.daily”, “/etc/hourly”, “/etc/cron.monthly”, and “/etc/cron.weekly”. 

If you are a system administrator, then you can edit the “/etc/crontab” file in your nano editor for defining Cron Jobs:

$ sudo nano /etc/crontab

What are User-specific Cron Jobs in Linux

The User-specific Cron Jobs are the types of Cron Jobs defined for a particular user. These types of Cron Jobs exist in the “/var/spool/cron/crontabs/” directory. You can manually edit the specified Cron Job files; however, it is recommended to use the “crontab -e” command to perform the edit operation:

$ crontab -e

The execution of the “crontab” command with the “-e” option will show you three options for editing the Cron Job file. We have selected “1” as it is easy to edit files in the nano editor:

Your user-specified Crontab file will somehow look like this:

Cron Jobs examples in Linux

In this section, we will show you some of the Cron Job examples that can help you understand the formation of the Cron Jobs.

Note: All of the scripts which are used in the below-given examples are created manually.

Example 1: The following crontab command is added in the crontab file for executing the “/root/backup.sh” script after every 40 minutes:

40 * * * * /root/backup.sh

After typing out the crontab command, press “CTRL+O” to save the added changes:

Example 2: In the second example, we will write a crontab command which will execute the “filescript.sh” at every 5:00 pm on 10th of May, August, September, and October:

00 05 10 5,8,9,10 * /usr/local/bin/filescript.sh

Example 3: The following crontab command will execute the “filescript.sh” script at 10:00pm every Saturday:

0 10 * * sat  /scripts/filescript.sh

Example 4: You can also specify multiple days in the crontab command. For instance, to execute our “filescript.sh” script at 4:00 pm every Sunday and Friday, we will write out this crontab command:

0 04 * * sun,fri  /script/filescript.sh

Example 5: In the following example, we have added a condition that helps to execute the “/scripts/filescript.sh” file every 30 seconds:

* * * * * /scripts/filescript.sh
* * * * *  sleep 30; /scripts/filescript.sh

Example 6: You can also use special Cron Job strings to schedule jobs. For instance, “@daily” is added at the start of the crontab command to schedule a Cron Job, which will run each day:

@daily /scriptdir/filescript.sh

Specifying “@yearly” in the following command will execute the “filescript.sh” script on yearly basis:

@yearly /scriptdir/filescript.sh

To schedule a Cron Job that will run the “filescript.sh” script file once a week, add the “@weekly” string at the start of the crontab command:

@weekly /scriptdir/filescript.sh

Similarly, you can use “@hourly” and “@monthly” strings for scheduling the Cron Jobs to run once an hour and once a month, respectively.

Example 7: You can also define Cron Jobs for a particular user in the open file. To do so, check out the below-given syntax:

*****  [Username] [Script_name]

Here, we will create a Cron Job that will execute the “scriptfile” present in our home directory for the “linuxfoss” user every 20 minutes:

20 * * * * linuxfoss scriptfile

Example 8: Want to run a backup of all of your user accounts? With the help of the crontab command, you can schedule a system backup. For instance, the below-given crontab command will create a Cron job to backup the user accounts at 6 am every week:

 0 6 * * 1 tar -zcf /var/backups/home.tgz /home/

Press “CTRL+O” for writing out the commands in the crontab file and then exit from the nano editor:

How to list Cron Jobs in Linux

Listing Cron Jobs in the terminal is useful when viewing all Cron Jobs at once. For this purpose, you can add the “-l” option in the “crontab” command and then execute it on your Linux terminal as follows:

$ crontab -l

In our crontab file, we have only saved the Cron Job for creating a backup for all user accounts, and it can be seen in the output as well:

How to create a backup of Cron Jobs in Linux

We have shown you the listing of Cron Jobs by using the “crontab” command. You can also redirect the output of “crontab -l” to a specific file for creating a backup of the added Cron Jobs. The STDOUT or the redirect operator “>” can be utilized to perform the specified operation:

$ crontab -l > backupfile.txt

Now, type out the “cat” command with your Cron Job backup file name to view its content:

$ cat backupfile.txt

How to remove Cron Jobs in Linux

The “-r” option is added in the crontab command for removing the Cron Jobs from the crontab file. Additionally, you can add the “-i” option for prompting the user for deleting the Cron Jobs:

$ crontab -i -r

To remove Cron Jobs without prompt, execute the simple “crontab -r” command, and you are all done!

$ crontab -r

Conclusion

Scheduling and automating repetitive processes boost productivity in a Linux-based system while reducing manual monitoring and user interruption. There exist enormous programs that help to schedule tasks; however, the “Cron” is the most extensively used. In the crontab file, we add our Cron Jobs by utilizing the “Cron” daemon. This write-up explained Cron Job’s syntax, related commands, and Cron Jobs types. Moreover, we have also demonstrated some Cron Jobs examples that can assist you in understanding the Crontab command structure.