Computer

This is the most difficult function of Linux, but you must know how to use it to work with it

The first two numbers represent the time in which the command will be executed, the first number being the minute and the second the hours. The time format used by Crontab is 24 hours, so if we want to perform the task at 5 in the afternoon, this would be the number 17. The asterisks represent all the values, while the number 0 corresponds to Sundays.

Do not confuse Cron with Crontab

While Crontab allows us to perform the automations of our system, these are executed, although not exclusively, with the Cron application, an application that runs in the background and that performs all the tasks that we have previously configured in Crontab. This application uses the time of our system to perform them, so it is important, if we use a virtual machine, that both the time of the guest machine and that of our team are always the same, since, otherwise, the scheduled time will not be correct.

Cron reads the content of the Crontab file every minute, so we can edit it at any time to perform tasks that we want to perform in the same session, without the need to force the application to read its content again to find new scripts to execute.

Not all users have the ability to create Crontab files to automate system tasks. The system administrator has the possibility to allow or deny this option through the file cron.allow or cron.den respectively, files that are located in /etc/cron.do /etc/ depending on the version of the Linux distribution that we have. installed on our computer.

How to automate tasks in Linux

As we have mentioned above, Crontab scripts start with the date we want them to be executed followed by the command. Crontab syntax is as follows.

Schedule Management

minuto hora día-del-mes mes día-de-la-semana ruta-comandos

  • Minute, this can be from 0 to 59
  • Time, as we have mentioned, Crontab uses the 24-hour schedule, so the range of numbers is from 0 to 23.
  • Day of the month, there is not much mystery, in this parameter since it goes from 1 to 31.
  • Month, from 1 to 12
    • january 1
    • 2 february
    • March 3
    • 4 april
    • May 5
    • June 6
    • July 7
    • 8 august
    • September 9
    • 10 october
    • 11 november
    • December 12.
  • Day of the week, from 0 to 6, with Sunday being the number 0, although some versions of Crontab also allow the number 7 to be used. In this way, the code for the days of the week is
    • 0 sunday
    • 1 monday
    • 2 tuesday
    • 3 wednesday
    • 4 thursday
    • 5 friday
    • 6 saturday

But, in addition, we can also use the asterisk

to specify all possible values ​​of a variable. If we want to set several values, we can do it through a comma (,) or a range of values ​​separating them with a hyphen (-). We can also set different steps with “/”.

  • If it is still not very clear, then we show you the time codes that we can use to perform tasks periodically depending on their frequency
  • 0 * * * * Using the timecode, the script will run every hour on the dot.
  • 0 0 * * * Every day the script will be executed only once
  • 0 0 * * 0 The script runs once a week
  • 0 0 1 * * Will run once a month

0 0 1 1 * The script will be executed once a year

crontab nombre-archivo

Once we are clear about how task scheduling works in Crontab, the first thing we are going to do is create the file where we are going to create all the processes that we want to automate using the following command

crontab -e

If we don’t want to create a new one, or we have already created it and we want to edit it, we use this command

crontab -d

If we want to delete the Crontab file created

crontab -d

With the following command, we can list all the tasks included in the crontab file

System administrators can limit the use of system automations by editing the cron.allow or cron.den files, which are located in /etc/cron.d/ or /etc/ if we have not previously changed their location.

automate tasks

nano nombrearchivo

To edit the Crontab file that we have created, we are going to use the text editor included in all Linux distributions, although we can also edit it directly from Crontab by adding the -e variable.

chmod ugo+x nombrearchivo

We introduce the numerical code accompanied by asterisks and the script that we want to execute. Next, we need to give Cron the permissions so that he can run it, otherwise he won’t know about it and will never be able to run it. To do so, we will use the command

Initially, using Crontab to automate tasks does not seem to be easy at all and is reminiscent of the (almost usual) Linux need to resort to the command line for practically everything, thus limiting more users from adopting Linux as an operating system on a day-to-day basis. .

The Windows task scheduler uses a graphical interface that is not very easy to use without the necessary knowledge, however, it is much more intuitive than the one offered by Linux through the command line.

Example of how Crontab and Cron work

crontab prueba

The first thing we must do to automate tasks in Linux is to create the file where we are going to include all the commands that we want to be executed on the computer at the time we configure. In this example, we are going to create the “test” file that will be in charge of deleting all the empty files and folders that are in the tmp directory. To do so, we will enter the following command.

find /tmp -type f -empty -delete

Next, we must enter the command (or commands separating them with semicolons “;” without the quotes). In this example, we are going to use Crontab to delete the files in the tmp folder.

crontab -e

Next, we edit the file to add the schedule, that is, when we want the tasks that we have entered in the file to be executed.

0 10 * * * find /tmp -type f -empty -delete

For it to be executed every day at 10 in the morning we must add before the script 0 10 * * *, leaving the line as follows If we want it to run at another time, we just have to modify the number 10 for the time (in 24-hour format) in which we want it to run. We can also use the time codes

[code]chmod ugo+x prueba

that we have shown in the Programming Management section to configure it. Finally, we use the following command so that Cron can run the automations file that we have created.

Alternatives to Cron

As we have mentioned in previous sections, Cron is in charge of executing the commands that we have previously entered in Crontab. Cron is a perfect tool for teams that are in operation 24 hours a day, although we can also adapt it to our work schedule, even if it subtracts resources from the team while the tasks are being carried out. If we are looking for simpler options to Cron, then we will show you some of the best alternatives.

anachron

sudo apt install anacron

Anacron allows us to schedule the automated execution of tasks in a day, week or month, at any time. If the computer is off, the next time we start the computer, the task that we have previously entered in Crontab will be carried out. With Cron, if the computer is turned off on the specified date, the task will not be carried out when we turn on the computer. Anacron is available in most Linux distributions and we can install it on the computer through the following command.

cronie

wget https://github.com/cronie-crond/cronie/releases/download/cronie-1.6.1/cronie-1.6.1.tar.gz

Cronie offers us a much more complete experience than Cron, and a small set of applications (including Anacron) and with which we can program workflows very quickly and easily through Crontab. Unlike Anacron, to install Cronie, it is not available in the Linux repositories, so we must visit its web page on GitHub to download it, or use the following command.

Related Articles

Leave a Reply

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