Automating tasks on Ubuntu 20.04 using Cronjobs
- cron
- cronjob
- crontab
- cronfile
- automate
- automation
- ubuntu
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:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console
- You have configured your SSH key
- You have created an Instance which is running Ubuntu Focal Fossa (20.04).
- You have sudo privileges or access to the root user.
Installing Cron
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.serviceThe 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
Understanding 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 /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:
- Months:
JAN-DEC
- Day of the week:
SUN-SAT
Below is an example that runs a backup every night at 4:30:
30 04 * * * /var/www/websites/backup.sh
Special strings
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 very 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 * * * * . |
Managing crontabs
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 -eIf 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 alternatively 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.
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 commandOnce done, press
CTRL
andO
to save your modifications, thenCTRL
andX
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.
Displaying your cronjobs
If you want to display the content of your crontab, but not edit it, use the following command:
crontab -l
Deleting your crontab
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.