Automating tasks on Ubuntu 20.04 using Cronjobs
Cron functions as a background process responsible for running scheduled commands on Unix-like operating systems, such as Ubuntu Linux 20.04 LTS (Focal Fossa). These commands, designated for execution, are stored within a structure known as a Crontab. The tasks executed by the Cron daemon are referred to as Cronjobs. By operating autonomously, Cronjobs enable the automation of maintenance-centric duties, facilitating the administration of your machines.
You may need certain IAM permissions to carry out some actions described on this page. This means:
Cron 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 for each user of the system. This allows each user to schedule their own cronjobs. These cronfiles are located in the
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|
You can also use the following notation for:
- Day of the week:
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:
|Wildcard variable that represents “all”, *|
|Break up scheduling values to form a list, |
|Range of values in the schedule field, |
|Express a step value,|
|Run once, at startup.|
|Run once a year, |
|(same as |
|Run once a month, |
|Run once a week, |
|Run once a day, |
|(same as |
|Run once an hour, |
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.
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.
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 :
Enter the number corresponding to the editor of your choice, or alternatively press
Enterto 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.Note:
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
Oto save your modifications, then
Xto 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:
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.