Scheduling cron jobs under Linux with crontab

cron job scheduling tutorial under Linux with crontab

On a Linux system, thanks to crontab, you can schedule the execution of tasks at specified intervals, called "Cron jobs". In this tutorial, we'll explore how to use Crontab to schedule Cron jobs on Linux.

Whether on a Windows or Linux server, it's not uncommon to have to schedule tasks to automate certain actions, particularly outside business hours. This may involve running a system update or launching a script to back up data or a database.

1. What is the crontab file?

The file crontab is a text file that specifies the schedule of Cron tasks to be run locally on the Linux machine. There are two types of Crontab files: the system Crontab files and user Crontab fileswhich influences the execution context. User Crontab files are stored in different locations depending on the Linux distribution.

If we take the example of Debian and Ubuntu, these files are stored in /var/spool/cron/crontabs. At the global system level, the crontab are located in /etc/crontab and scripts in the /etc/cron.d.

Although you can modify Crontab files manually, it is recommended that you use the command crontab provided for this purpose.

We'll now move on to the syntax of Crontab files to understand how to correctly specify task execution times.

2. Crontab syntax

Each line of a crontab file corresponds to a scheduled task and must follow a precise structure. In fact, each line contains six fields separated by a space, the last value being the command to be executed. These fields represent the minute, l'hourthe day of the monththe monththe DAY OF WEEKand finally the order.

Here is an example of a typical Crontab line:

<minute> <heure> <jour du mois> <mois> <jour de la semaine> <commande>
  • Le premier champ représente la minute (0-59).
  • Le deuxième champ représente l’heure (0-23).
  • Le troisième champ représente le jour du mois (1-31).
  • Le quatrième champ représente le mois (1-12).
  • Le cinquième champ représente le jour de la semaine (0-7, où dimanche = 0 ou 7).
  • Le dernier champ contient la commande à exécuter.

By modifying these values, it will be possible, for example, to execute a :

  • Toutes les 10 minutes
  • Tous les lundi de chaque semaine à 06:00
  • Tous les jours à 01:00
  • Toutes les deux heures, le samedi et le mercredi
  • Etc…

It's a very flexible system. You should also note the following about First 5 fields of a cron job They can contain one or more values, separated by commas, or ranges of values separated by a dash. The asterisk is also an option.

The following operators are available:

  • * : l’astérisque représente toutes les valeurs. Par exemple, si vous placez un astérisque sur le champ correspondant au jour de la semaine, cela signifie que la tâche s’exécutera tous les jours.
  • , : la virgule permet de spécifier une liste de valeurs, pour une exécution répétée. Si vous avez 7,23 dans le champ des heures, cela signifie que la tâche s’exécutera à 7h du matin et 23h du soir.
  • : le tiret permet de définir une plage de valeurs.
  • / : le slash permet de spécifier des intervalles de valeurs. Par exemple, le fait de préciser */15 dans le champ des minutes signifie que la tâche sera exécutée toutes les 15 minutes.

Let's see how to use these operators and combinations in concrete examples.

3. Predefined macros and Crontab commands

It is very common to schedule recurring tasks via the crontab. Rather than having to write complete lines with all 6 fields, Linux developers had the excellent idea of creating predefined macros.

For example, the macro @daily runs a task every day at midnight:

@daily order

Here is a list of available macros:

  • @yearly pour exécuter la tâche une fois par an, à minuit, le 1er janvier.
  • @monthly pour exécuter la tâche une fois par mois à minuit, le 1er jour de chaque mois.
  • @weekly pour exécuter la tâche une fois par semaine, à minuit, chaque dimanche.
  • @daily pour exécuter la tâche tous les jours à minuit (00:00).
  • @reboot pour exécuter la tâche au démarrage du système.

4. Create a task in the crontab

How to order crontab allows you not only to create and modify Crontab files, but also to view or delete them. Here are some useful commands:

  • crontab -e : ouvrir ou créer un fichier Crontab pour l’édition.
  • crontab -l : afficher le contenu du fichier Crontab.
  • crontab -r : supprimer le fichier Crontab actuel.

Let's explore some concrete examples of Cron tasks on a Linux machine. First, open the system crontab :

sudo crontab -e

If you do not specify sudoto edit the crontab of the user you are currently logged in with. The first time, you need to select the text editor with which to edit the crontab. Specify 1 to choose nano.

Next, you need to declare your cron job at the end of the file, after any comments.

Execute an order every day of the week, except Saturday and Sunday, at 06:00:

0 06 * * 1-5 /path/to/order

Run a Bash script every 15 minutes and redirect the standard output to /dev/null :

*/15 * * * * /home/tutomaker/script.sh > /dev/null

Once the line has been added to the crontab, save with the shortcut CTRL + O and close with CTRL + X (if you use nano).

To familiarize yourself with the planning system, I recommend you use this site:

5. Conclusion

After reading this article from the IT tutorial box, you now know how to schedule and automate tasks under Linux using Crontab. This applies to Debian, Ubuntu, Fedora, AlmaLinux, Rocky Linux and many other Linux distributions.

Whether you want to automate simple daily tasks or run custom scripts, you need to use crontab. Unless you prefer to run the task manually every time it's needed? I don't think so...

You may also like...

Leave a Reply

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