/etc/crontab: Explanation & Insights

Execute commands on a regular basis

/etc/crontab is a system file in Unix-like operating systems that contains instructions for the cron daemon, which is a time-based job scheduler. The file contains a list of commands that are executed automatically on a regular basis.

The format of the /etc/crontab file is as follows:

*     *     *     *     *     username  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of the week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of the month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Each line in the file represents a command that should be executed at a specific time. The first five fields specify the time and date when the command should be run, and the sixth field specifies the user account under which the command should be executed. The seventh field contains the command itself.

Here are some examples of how to use /etc/crontab:

Run a backup script every day at midnight:

0 0 * * * root /usr/local/bin/backup.sh

This command runs the backup.sh script located in the /usr/local/bin directory as the root user account every day at midnight.

Run a system update every week on Monday at 3:30 AM:

30 3 * * 1 root apt-get update && apt-get upgrade -y

This command updates the system every Monday at 3:30 AM as the root user account using the apt-get command.

Clean up the temporary files every hour:

0 * * * * root rm -rf /tmp/*

This command deletes all files in the /tmp directory every hour as the root user account.

Note that the syntax for the crontab file is very strict, and even small errors can cause it to fail. It is important to double-check the syntax before saving any changes to the file.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA