The Cron system in Linux is essentially an automatic task scheduler.

Its name comes from the Greek word chronos, meaning time.

Cron is the service or the entire system. It’s the mechanism that ensures things run on time, which is constantly checking to see if there is a scheduled time or date for which it needs to perform an action.

On the * * * * * command line, the Cron is the system that interprets the five asterisks and tells the system: “It’s time to run the command”

A job is the specific action that the Cron system is scheduled to execute. It’s the content of the line you write in the crontab.

It’s the instruction: It’s what you want done. It’s usually a shell command, a script, or the execution of an application, In the line * * * * * /usr/bin/date > /tmp/date.txt, the Job is the command /usr/bin/date > /tmp/date.txt

Cron is the when and how of scheduling, while a Job is the what to do. A Job is executed by the Cron system.


The standard Linux crontab utility (the one you use with crontab -e or crontab file.txt) is designed to have only one configuration file per user.

The main function of Cron is to run commands or scripts recurringly and automatically on a predefined schedule.

The Daemon is called crond or cron (The final “d” in Linux systems is because it is a “daemon”).

Function: It’s a service that constantly runs in the background. Every minute, crond checks its configuration files (crontabs) to see if any tasks are scheduled to run at that time.

The smallest unit of time that cron handles is 1 minute, it cannot handle seconds.

A crontab is a text file where scheduled tasks are stored. Each user on the system has their own personal Cron table.

Users interact with their tasks using the command

crontab -e

When you exit the file editor, the crontabs will be installed.

the console will tell you:

crontab: installing new crontab

The first time you open crontab you will be asked to choose a text editor (such as nano or vi), its numeric option

or, if you have a file with cron regular expressions:

crontab file

important: It is not the same to execute a command with the reserved word “sudo”

for the user

crontab -e

is different from (for the root user):

sudo crontab -e

While one command displays tasks for the user, the other displays tasks for the root user (their privileges change).

Cron configuration files, known as crontabs, are stored in specific, separate locations within the Linux file system.

Both files are typically located in directories that manage temporary or spooled system data


To modify crontabs files directly you must log in as the root user.

You must be very careful with the syntax of the crontabs, if there is something wrong you can see the error “rename: Operation not permitted” and you must delete the crontabs file with the root user

The crontab -e command doesn’t edit the file directly. It edits a temporary copy and then installs it to this location

locations:

the folder that contains all:

/var/spool/cron/crontab

files that crontabs save:

  • /var/spool/cron/crontab/root
  • /var/spool/cron/crontab/[username]

There is a third location for tasks that do not belong to any specific user, but to system applications or services.

/etc/cron.d

This directory contains separate files (not a single crontab file) where each file defines tasks to be run


Syntax (The Schedule)

Each line of a crontab follows a strict pattern of six fields:

* * * * * COMMAND_TO_EXECUTE
| | | | |
| | | | +--- Day of the week (0-7, 0 and 7 is Sunday)
| | | +----- Month (1-12)
| | +------- Day of the month (1-31)
| +--------- Hour (0-23)
+---------- Minute (0-59)

examples

30 14 * * * /usr/bin/backup.sh

Run the backup script at 2:30 PM (14:30), every day of the month, every month, and every day of the week.

0 */4 * * * /usr/bin/script.py

Run the script every four hours (at 0:00, 4:00, 8:00, 12:00, 16:00, 20:00).

@reboot /usr/bin/cleanup.sh

Run the cleanup script every time the system reboots (@reboot is a cron keyword).

Suppose you create the following crontab at 20:32

*/10 * * * * date +\%H:\%M:\%S >> /home/username/a

At 9:00:00 PM, the cron service will run the command date +\%H:\%M:\%S >> ~/a for the first time, creating the file a in your home directory and writing the time.

The crontab user must have privileges to execute that command (in that case, a file “a” is created if it does not exist and the content “time” is appended)

When you use crontab file.txt, it completely replaces your existing scheduled tasks list with the contents of the new file.


If you want to add tasks from a new file (new_cron.txt) without deleting the tasks you already have active, you must manually merge them before uploading.

This process ensures that you never miss a task and that the newly uploaded file (crontab.final.txt) contains all the tasks you want.

download your current crontab (the one you have active) to a temporary file:

crontab -l > current_crontab.txt

cron root:

sudo crontab -l > current_crontab_root.txt

Use the cat command to merge your current task file and your new file into a single final file:

cat current_crontab.txt new_cron.txt > final_crontab.txt

Now, use the combined file to replace all tasks, which is actually an update containing both the old and the new:

crontab final_crontab.txt

Below is a cron that writes “hola mundo” (“hello world” in spanish) to the file “mi_log.txt” every minute and adds what I add with a line break, it does not overwrite the file.

the cron regular expression is:

* * * * echo "hola mundo" >> /home/username/mi_log.txt

/home/username is ~/

>> send content and adds and does not overwrite, > overwrites content in file


video in Spanish to understand:


cron commands:

Open your personal crontab file in your default text editor (such as vi or nano) to add or modify tasks:

crontab -e

Displays the contents of your current crontab file (the scheduled tasks you have running).

crontab -l

Deletes your entire user crontab file, removing all scheduled tasks. Use with caution!

crontab -r

Replaces your entire current crontab with the tasks defined in the specified file (file.txt).

crontab file.txt

Lists scheduled tasks for a specific user (e.g., crontab -u xxxx -l). Requires root permissions.

crontab -u username -l

Check the status of the Cron daemon (crond or cron) to ensure the service is running correctly.

sudo systemctl status cron

Reinicia el servicio Cron. Esto es útil si has modificado la configuración del sistema (como los archivos en /etc/cron.d) y quieres que los cambios se apliquen inmediatamente.

 sudo systemctl restart cron