Skip to main content

What are Cron and Crontab? What are they used for?

Many of you have probably come across these two terms and wondered what they are and what functions they serve. Both are processes exclusive to UNIX-like systems such as Linux and OpenBSD, among others.

Crontab is a file that stores tasks we want to automate, such as:

  • making backups
  • deleting files
  • fixing system errors
  • updating the system
  • and other tasks

Cron uses this file and checks periodically whether a task has been executed or is still running, and also how long ago it was run. If a task hasn’t been executed yet, Cron will run it autonomously. Such tasks are called cronjobs. Note that everything runs in the system shell (terminal).

This might sound confusing at first, so let me clarify with a concrete example: Imagine you want a script to run every half hour, and this script performs backups of a folder. You must “tell” Cron that this script needs to be run every 30 minutes. Cron then stores this information in a crontab file and periodically checks if the script is running. If it’s not running, Cron executes it; if it is already running, Cron does nothing but continues to monitor the task in case of anomalies, in which case it will rerun it.


Two ways Cron can act:

  • Global
  • Local

Global means the system root user defines the crontab tasks that apply to all users.

Local means Cron only acts on the account that created it.


Crontab commands for user management:

  • -l user — lists scheduled tasks for the user
  • -e user — edits the user’s crontab
  • -r user — deletes the user’s crontab
  • -c directory — specifies a directory for crontab files

To check all crontabs of a user, look in this directory: /var/spool/cron/user

To edit global crontabs, edit the file: /etc/crontab


Crontab syntax

Crontab entries are divided into 6 fields:

Campo Valores Descrição
Minuto 0–59 Minuto da hora
Hora 0–23 Hora do dia
Dia do Mês 1–31 Dia do mês
Mês 1–12 Mês
Dia da Semana 0–7 (Dom-Sáb) 0 e 7 representam o domingo
Comando Comando a executar

For example, to run a script every day at 16:00 (4 PM), use:

0 16 * * * /home/dev/backup.py

Special characters in crontab

  • Asterisk (*) Selects all possible values in the field. For example, * in the hour field means every hour.

  • Slash (/) Specifies increments. For example, 59/2 in the hour field means the task runs every 2 hours starting at minute 59.

  • Comma (,) Separates multiple values. For example, 1,2,3 in the month field means January, February, and March.

  • Hyphen (-) Defines a range. For example, 15-23 in the hour field means hours 15 through 23 inclusive.

  • Question mark (?) Used to omit either day of month or day of week. It can only be used in one of those fields.

  • L Indicates the last day of the week or month. For example, L in the day of week field means Sunday (7).

  • W Used in the day of month field to specify the nearest weekday to a given day. For example, 28W means the nearest weekday to the 28th of the month. Note that Saturday and Sunday are not considered weekdays.

  • # (hash) Used only in the day of week field. Specifies the nth occurrence of a weekday in the month. For example, #4 means the 4th occurrence of the specified weekday.


I hope this clarifies the basics of cron and crontab for you!