Feb 20, 2023
Automate and Schedule Tasks on Linux With Cron
cron
is a time-based task scheduler that allows developers and system administrators to automate the execution of commands and scripts at specific times or intervals, down to the minute.
Install cron
On Linux, cron
can be installed using the apt
command:
$ sudo apt install cron -y
And launched as a background job (i.e. daemon) using the service
command:
$ sudo service cron start
The crontab file
The tasks managed by cron
are listed in a file named a crontab, that has the following format:
m h dom mon dow command
Each line of this file represents a distinct cron task, which is composed of five fields representing the time to execute the command, followed by the shell command to execute.
The time fields can take the following values:
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 (0 - 7), Sunday being both 0 and 7.
Or the *
symbol, which translates to any possible values.
For example, the following cron
task will perform a dump the MySQL stats
database every Sunday at 10:00 AM:
0 10 * * 7 mysqldump --databases stats > /tmp/dump.sql
And this one, will execute the check.sh
script every minute:
* * * * * /home/razvan/scripts/check.sh
Manage cron tasks
Create and edit cron tasks
To create or edit the crontab file, you can use the crontab
command with the -e
option flag, which will open the crontab file using the default text editor.
$ crontab -e
If you want to use a specific text editor instead, you can define it in the local EDITOR
environment variable, right before the crontab
command.
$ 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
List cron tasks
To display the content of the crontab file, you can use the crontab
command with the -l
option flag:
$ crontab -l
Cron tasks and 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 root
crontab file:
$ sudo crontab -e
Environment variables
By default, cron tasks will run without a defined environment.
For example, if you want to execute the following task that requires the node
binary:
0 10 * * * node ~/script.js
cron
will not run this task as it won't know where to find that binary, due to the absence of the PATH
variable in its execution environment.
You can therefore either use the absolute path of the node
binary:
0 10 * * * /usr/bin/node ~/script.js
Which can be found using the which
command:
$ which node
/usr/bin/node
Or define the PATH
environment variable right before the command:
0 10 * * * PATH=/usr/bin node ~/script.js
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.