You can also use the following notation for:
- Months:
JAN-DEC
- Day of the week:
SUN-SAT
Cron serves as the silent orchestrator behind the scenes on Unix-like systems like Ubuntu Linux 20.04 LTS (Focal Fossa), dutifully executing scheduled commands stored within the Crontab structure. These scheduled tasks, fondly called Cronjobs, work tirelessly to automate routine maintenance chores, streamlining machine administration with their autonomous operation.
To complete the actions presented below, you must have:
sudo
privileges or access to the root userCron is part of the Ubuntu operating system and is preinstalled on most Ubuntu distributions. However, you can manually install Cron using the APT package manager if necessary.
Make sure your APT package index is updated:
apt update
Install Cron using the APT package manager:
apt install cron
Make sure the service is enabled and running in the background to be able to schedule tasks:
systemctl enable cron.service
The following output displays:
Synchronizing state of cron.service with SysV service script with /lib/systemd/systemd-sysv-install.Executing: /lib/systemd/systemd-sysv-install enable cron
Cronjobs are managed from a cronfile, specific to each user of the system. This allows each user to schedule their own cronjobs. These cronfiles are located in the /var/spool/cron/crontabs/
directory.
Cron allows you to run almost any command you would normally run from the command line.
Each crontab uses the following structure:
MINUTE (0 - 59) | HOUR (0 - 23, 0 = MIDNIGHT) | DAY OF THE MONTH (1 - 31) | MONTH (1 - 12) | DAY OF THE WEEK (0 - 6, 0 = SUNDAY) | COMMAND OR DIRECTORY PATH AND SCRIPT NAME |
---|---|---|---|---|---|
* | * | * | * | * | e.g. /var/www/websites/backup.sh |
You can also use the following notation for:
JAN-DEC
SUN-SAT
Below is an example that runs a backup every night at 4:30:
30 04 * * * /var/www/websites/backup.sh
Cron provides a range of special strings, which can be used in place of the five time-and-date fields:
String | Meaning |
---|---|
* | Wildcard variable that represents “all”, * * * * * run every minute of every hour of every day of every month. |
, | Break up scheduling values to form a list, 0,30 * * * * run at the beginning and the middle of every hour. |
- | Range of values in the schedule field, 0-29 * * * * run every minute of the first 30 min of every hour. |
*/ | Express a step value,*/10 * * * * run on every minute divisible by 10 (10, 20, 30 etc.). |
@reboot | Run once, at startup. |
@yearly | Run once a year, 0 0 1 1 * . |
@annually | (same as @yearly ) |
@monthly | Run once a month, 0 0 1 * * . |
@weekly | Run once a week, 0 0 * * 0 . |
@daily | Run once a day, 0 0 * * * . |
@midnight | (same as @daily ) |
@hourly | Run once an hour, 0 * * * * . |
Once you have decided when you want to run your cronjobs, provide the info in your crontab
file so that the daemon can read it.
The crontab
file is a regular text file, however, it is not recommended to edit it directly. Use the crontab
command instead, which will also check your edits for syntax errors.
Open the crontab
file:
crontab -e
If you are running the command for the first time, and no crontab exists for your user, a new one will be created. The following prompt displays:
Select an editor. To change later, run 'select-editor'.1. /bin/nano <---- easiest2. /usr/bin/vim.basic3. /usr/bin/vim.tiny4. /bin/edChoose 1-4 [1]:
Enter the number corresponding to the editor of your choice, or press Enter
to use the default choice, nano
- which is the most user-friendly option.
Edit your crontab by adding your tasks at the end of the file.
By default, the file contains some commented-out information on how to edit the crontab file. Go to the end of the file to add your tasks.
# Edit this file to introduce tasks to be run by cron.## Each task to run has to be defined through a single line# indicating with different fields when the task will be run# and what command to run for the task## To define the time you can provide concrete values for# minute (m), hour (h), day of month (dom), month (mon),# and day of week (dow) or use '*' in these fields (for 'any').## Notice that tasks will be started based on the cron's system# daemon's notion of time and timezones.## Output of the crontab jobs (including errors) is sent through# email to the user the crontab file belongs to (unless redirected).## For example, you can run a backup of all your user accounts# at 5 a.m every week with:# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/## For more information see the manual pages of crontab(5) and cron(8)## m h dom mon dow command
Once done, press CTRL
and O
to save your modifications, then CTRL
and X
to quit the text editor.
crontab
will remember your choice of editor. When you run crontab -e
in the future, it will open the file automatically in the same text editor.
If you want to display the content of your crontab, but not edit it, use the following command:
crontab -l
If you want to delete your crontab, run the following command:
crontab -r -i
When prompted, press y
to confirm the deletion, or n
to cancel the process.
For more information about cron jobs, refer to the official documentation.