The cron and crontab commands in Linux: scheduling periodic tasks

Introduction

In Unix-like operating systems, automating repetitive tasks is essential to save time and reduce human errors. The cron daemon allows executing commands or scripts at predefined times, while crontab is the interface each user uses to define their own schedule table. In this article we will see how cron works, the syntax of crontab, and several practical examples you can apply immediately on your server or workstation.

What is cron?

Cron is a background process that runs continuously and checks, every minute, if there is any scheduled entry that matches the current time. When it finds a match, it launches the associated command with the privileges of the user who scheduled it. Its design is simple yet powerful: it does not require graphical interfaces and works on practically any Linux distribution.

How does crontab work?

Each user has their own crontab (cron table) file stored in /var/spool/cron/crontabs or, on more modern systems, managed via the crontab -e command. By editing this file, you define a list of lines where each line represents a task and consists of five time fields followed by the command to execute.

Crontab syntax

The basic structure is:

* * * * * command_to_run
- - - - -
| | | | |
| | | | +--- day of week (0‑7, where 0 and 7 are Sunday)
| | | +----- month (1‑12)
| | +------- day of month (1‑31)
| +--------- hour (0‑23)
+----------- minute (0‑59)

The asterisks indicate ‘all values’. Additionally, ranges, lists, and steps can be used.

This post is also available in ESPAÑOL.

Leave a Reply

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

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .