Scheduling Tasks With Cron

8 min read·Jan 1, 2025

cron is a time-based task scheduler that allows developers and system administrators to automate the execution of commands and scripts at specific dates, times, or intervals, down to the minute.

These cron jobs are often used to schedule system updates, perform data backups, send emails, clear log files, and so on.

Installing cron on Linux

To install cron on Linux, you can use the following apt command:

$ sudo apt install cron -y

And launched it as a background job (i.e. daemon) using the service command:

$ sudo service cron start

Note: cron is already included in macOS distributions.

The crontab format

The tasks managed by cron are listed in a file named a crontab, where each line represents a distinct task:

m h dom mon dow cmd

Where:

  • m is the minutes in the hour (0 - 59).
  • h is the hours of the day (0 - 23).
  • dom is the day of the month (1 - 31).
  • mon is the month of the year (1 - 12).
  • dow is the day of the week (1 - 7), Monday to Sunday.
  • cmd is the command to execute.

Note: Except for the command, every other field can be replaced by a wildcard character *, which translates to any possible values.

Example

This cron task will remove the all the files present in the /tmp directory every Sunday at 10:00 AM:

0 10 * * 7 rm -rf /tmp/*

Specifying intervals and ranges

To specify every "nth" value, you can use the slash character /:

*/n * * * * cmd

To specify a list of values, you can use the comma character ,:

* * n,m * * cmd

To specify a range of values, you can use the dash character -:

* n-m * * * cmd

Note: All three syntaxes can be used together.

Example

This cron task will run every 5 minutes:

*/5 * * * *

This cron task will run at 9:00 AM, 12:00 PM, and 3:00 PM every day:

0 9,12,15 * * *

This cron task will run every hour from 9:00 AM to 5:00 PM every day:

0 9-17 * * *

Creating and editing cron jobs

To create or edit the crontab file, you can use the crontab command with the -e flag (short for "edit"), which will open the crontab file using your default text editor:

$ crontab -e

If you want to use a specific text editor instead, you can use the local EDITOR environment variable:

$ EDITOR=vim crontab -e

Once you're done editing this file, you can save and close it, which will cause the cron job to automatically apply the changes you've made:

crontab: installing new crontab

Listing cron jobs

To display the content of the crontab file, you can use the crontab command with the -l flag (short for "list"):

$ crontab -l

User permissions

Each user on the system has their own crontab file, named after their username, which can be found in the /var/spool/cron/crontabs directory:

$ sudo ls -l /var/spool/cron/crontabs
-rw------- 1 razvan crontab 1213 Feb  8 14:44 razvan

The tasks listed in a user’s crontab file will be executed at the scheduled time using the system-wide permissions defined for that specific user.

To run tasks that require elevated privileges (i.e. root access), such as updating packages with apt, you can use the sudo command in conjunction with the crontab command to edit the crontab file that belongs to the root user:

$ sudo crontab -e

Environment variables

By default, cron doesn't run with the user's environment but with a minimal environment with a limited PATH variable, which on most systems includes the /usr/bin, /bin, /usr/sbin, /sbin directories.

Consequently, if the specified command is not found within these directories, the cron job will fail.

To execute a command or script whose file is not located in one of these directories, you can either specify its absolute path:

* * * * * /path/to/command

Or specify the absolute path to its parent directory through the local PATH environment variable:

* * * * * PATH=/path/to/directory command

It goes without saying that the same method applies to any other environment variable that might be required for the proper execution of your command.

Note that to find the absolute path of a binary on the system, you can use the which command:

$ which command

Example

In this example, cron will not be able to execute the backup.sh script located in the /home/razvan/scripts directory due to its absence in the PATH variable:

0 10 * * * backup.sh

However, in this example, cron will be able to execute it when its absolute path is specified:

0 10 * * * /home/razvan/scripts/backup.sh

Summary

Here's a summary of what you've learned in this lesson:

  • Cron is a time-based task scheduler that allows developers to automate the execution of commands and scripts called jobs.
  • Cron jobs are stored in a file called a crontab located in the /var/spool/cron/crontabs directory.
  • Each crontab file is named after and belongs to the user who created it.
  • Cron jobs are by default executed with an empty environment and the permissions of the user it belongs to.
  • The crontab -e command is used to edit the user's crontab file.
  • The crontab -l command is used to list the jobs of the user's crontab file.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Scheduling Tasks With Cron in Bash | Backend Brewery