Crontab Manual – Scheduling Tasks in Linux

Carrying out the same tasks over and over again, whether on a daily, weekly or monthly basis, is a task that, over time, tires and degenerates, depending on its complexity, in which we leave it aside. The solution to this problem is to automate it as much as possible. Just like Windows allows us to automate certain tasks, Linux also offers us this option through Crontab, but not exclusively.

If we want to automate the operation of Linux, be it to make a backup copy, to generate reports, to modify the configuration or for anything else, the solution is to use Crontab in combination with Cron, since, unlike Windows, we need two applications minimum.

Crontab Manual

What is Crontab?

Crontab is a text file where automations are created based on a series of parameters, a text file that is read by Cron . Both applications are available natively in most of the most common Linux distributions, so it is not necessary to install any repository on our computer to be able to use them.

Crontab offers us a series of scripts, one per line, that allows us to customize the exact time in which we want to perform one or multiple tasks and their repetitions over time. In fact, its name derives from the Greek word Cronos , which means time. Each script contains information about the exact date on which it should be executed, which date is indicated by a series of numbers. For sample, a button. The following script will run nfsfind every Sunday at 5:30 in the morning

30 5 * * 0 /usr/lib/fs/nfs/nfsfind

The first two numbers represent the hour 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 carry out our system’s automations , 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 our system time 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 computer are always the same, since, otherwise, the programmed 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 we want to perform in the same session, without having to force the application to read its content again to find new scripts to run.

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 found 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. The 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 24-hour time, 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
    • February 2
    • March 3
    • April 4
    • May 5
    • June 6
    • July 7
    • August 8
    • September 9
    • October 10
    • November 11
    • December 12.
  • Day of the week, from 0 to 6, 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 the possible values of a variable. If we want to establish several values, we can do it through the 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 periodicity

  • 0 * * * * Using the time code, the script will be executed every hour on the hour.
  • 0 0 * * * Every day the script will be executed only once
  • 0 0 * * 0 The script runs once a week
  • 0 0 1 * * Will be executed once a month
  • 0 0 1 1 * The script will be executed once a year

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 we want to automate using the following command

crontab nombre-archivo

If we do not want to create a new one, or we have already created it and want to edit it, we use this command

crontab -e

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

crontab -d

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

automate tasks

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.

nano nombrearchivo

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

chmod ugo+x nombrearchivo

Initially, using Crontab to automate tasks does not seem to be easy at all and is reminiscent of Linux’s (almost common) 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.

Crontab and Cron working example

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 responsible for deleting all the files and empty folders found in the tmp directory. To do so, we will enter the following command.

crontab prueba

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.

find /tmp -type f -empty -delete

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.

crontab -e

In order for it to be executed every day at 10 in the morning, we must add 0 10 * * * before the script, leaving the line as follows

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

If we want it to be executed at another time, we just have to modify the number 10 for the hour (in 24-hour format) in which we want it to be executed. We can also use the time codes that we have shown in the Programming management section to configure it. Finally, we use the following command so that Cron can execute the automations file that we have created.

[code]chmod ugo+x prueba

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 running 24 hours a day, although we can also adapt it to our work schedule, even if it takes resources away from the team while tasks are being carried out. If we are looking for simpler options to Cron, then we show you some of the best alternatives.

anacron

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

sudo apt install anacron

Cronie

Cronie offers us a much more complete experience than Cron, and a small set of applications (among which Anacron is included) 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.

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