Crontab on linux what is it, what is it for and how to use it to schedule tasks?

To manage repetitive tasks, it is necessary to schedule the processes with a temporary frequency. For this the cron command is used, which is helped by a file called Crontab.

This file establishes, for each user, the way in which the processes must be managed. But this is not all you should know about Crontab, since it has other secrets that you should keep in mind.

If you want to know more about Crontab, we invite you to continue reading this post. You will know what type of tasks can be programmed and the steps you must do in the Linux Operating System .

What are Crontab commands and what are they for in Linux?

Before knowing how the Crontab command works, it is necessary to know what another command, the cron. The latter is a program that is used, within a Linux terminal, whenever it is necessary to execute processes in the background at a precise moment. This means that the action will take place (for example) every minute, every day, every certain number of hours or once a week.

To specify how cron should work, you need to write the instructions in a specific file, called crontab. This element is a text file that is saved within the command and that can be individualized and personalized by each user. From all this it follows that Crontab is a file that is saved within the cron command to specify the instructions that are necessary to carry out the task of the main command.

What kinds of tasks can be scheduled using the Crontab commands in Linux?

Among the tasks that can be scheduled using Crontab in Linux are: 

  • Activate a notification to remember events at a certain time.
  • Updating programs by setting the day and time you want this process to run.
  • Start your Linux antivirus in the background at a specific time.
  • Delete files that are in shared folders .
  • Customize user permissions every certain period.
  • Save a file at an exact time of the day.
  • Ask users to access password once a week.

Learn step by step how to use Crontab to automate tasks in Linux

The step by step you must do to use Crontab correctly and thus automate tasks in Linux is the following:

Start Crontab

The first thing you will have to do is create a script, which will work with cron and thus can follow the instructions you want the command to perform at a certain time. We will take as an example that you need the operating system update to be done automatically.

For this you will have to enter the console and write:

#! / bin / bash

#script name of automatic update mode

#write your Linux distribution

# apt-get update & apt-get -y upgrade (in case your distro is Ubuntu or Debian, you will have to remove #)


#yum -y update


#pacman --noconfirm -Syu

After you have created this script you will have to save it as and change the execution permissions by typing chmod a+x ~/scripts/ .

Add tasks

What you will have to do now is include what you want to be done from time to time (in our example it is the OS update, but you can also run the antivirus, check email, etc). To add a task you will have to use the argument -e, which will help you choose a text editor.

This will allow the Crontab file to be:

  • # m h dom mon dow user command

In this way you will have to enter the corresponding minutes to execute the script (m), the exact time (h), the day of the month that the task will be carried out (sun) and the day of the week (dow, you can also be numeric writing 1 as Sunday). For more information, look carefully at the image in this post, it will help you understand all the variables. You will also have (user)to enter the username and path to access the script (command).

Examples of this are:

  • To update every day at 11.30 am the script will have to write 30 11 * * * usuario /home/usuario/scripts/ .
  • If you want to run it on November 20 at 6.30 p.m. you will have to write 30 18 11 20 sun usuario /home/usuario/scripts/ .

If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will surely be of great help to more members of the community. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button