How to Set Up and Use Cron Jobs in Linux

Cron jobs in Linux are a fantastic utility to schedule the tasks for specific dates and times. You can use it to efficiently perform repetitive tasks automatically, reducing the chances of human error. The common applications of cron jobs include taking the system backups, maintenance, clearing the cache, and data synchronization.

You can also use the cron job for different tasks like automating the command execution in Linux. However, as beginners, many users face multiple errors while setting up a cron job. So, in this detailed tutorial, we will explain the different examples on how to set up and use cron jobs in Linux.

How to Set Up and Use Cron Jobs in Linux

Let’s divide this section into multiple parts to explain the approaches to set up, use, and modify the cron jobs in Linux.

1. How to Create a Cron Job
When creating a cron job, you must access the crontab, the table of the currently scheduled tasks on your system. Adding those tasks in the crontab is the only way of creating the cron jobs, and you can do it using the following command:

Now, verify that the cron service is running correctly on your system using the following given command:

In the terminal, type “crontab –e” which is the command to edit a cron table.

When you use the previous command for the first time, the system will ask you to select a text editor. As you choose an editor, it opens a file with basic instructions as shown in the following image:

You need to insert your task using the crontab expression which is * * * * */location/script. Each respective “*” here represents minutes, hours, day of month, month, and day of week. Furthermore, the location and script here represent the location and the name of the script that you want to run at the scheduled time.

2. Format of Time in Cron Jobs
Before entering the crontab expression, you must know its format which is:

1. Minutes: 0 to 59 where 0 and 59 are the visible minutes on the clock. If you enter 17 in the “minutes” field, the task will execute at 17 minutes every hour.

2. Hour: 0 to 23 where 0 and 23 represent 12 AM and 11 PM. For an input value of 2, the job will be scheduled for 2 AM every day. Please note that you should type “14” for 2 PM.

3. Day of month: 1 to 31 where 1 and 31 are the first and last days of the month. For the input value of 12, the execution will occur on the 12th day of every month.

4. Month: 1 to 12 where 1 and 12 are for January and December. When you enter a value in the “month” field, the task will execute in that particular month of the year.

5. Day of week: 0 to 7 where 0 and 7 are for Sunday. For example, in case you provide “5”, it will be scheduled for Friday, weekly.

Note: If you set any field as “*”, the code will consider every input for that field. For instance, if you enter “*” for a month, the command will run every month.

For example, to schedule a cron job for 5:30 PM on Mondays, your command will be:

30 17 * * 1 /<location>/<script>

For example, to schedule a cron job for noon on weekdays in February, the command will be:

0 12 * 2 15  /<location>/<script>

3. Use of Arithmetic Operators to Create Cron Jobs
As an administrator or developer, you’d often need to create a cron job to run quarterly, more than once a week, etc. Hence, instead of making multiple cron jobs, you can define that in a single command using the arithmetic operators that are listed as follows:

1. Asterisk(*): An asterisk indicates that the script should run for every field value. For instance, an asterisk in the “hour” field would mean that the task should run every hour.

2. Dash(-): You can use a dash to specify a range of values. For example, to set up a cron job for January to April, enter * * * 1-4 * /location/script.

3. Comma(,): Use a comma to separate different values. For example, to schedule a job for Monday and Friday, use * * * * 1,5 /location/script.

4. Forward Slash(/): Use “/” to divide a value into multiple values. For example, if you want your task to execute every third day, use * * */3 * * /location/script.

How to Manage a Cron Job

Managing a cron job is an easy task. Here are a few commands that are enough for you to perform the operations like listing, editing, or deleting:

1. To list the cron jobs, run crontab -l.

2. To remove all cron jobs, run crontab -r.

3. To edit a cron job, run crontab -e.

If you want to perform these operations for other users of the system, add the username in between the commands, like crontab -u username -l.

Conclusion

For Linux users and administrators, cron jobs are vital for repetitive tasks. You can set up cron jobs to run a script or command at a specific time, reducing a chunk of your unnecessary workload. In this article, we discussed how to create a cron job in Linux and what time format you can use within the fields. Moreover, we explained the method to add multiple time values within a cron job.

source

Leave a Comment