Automating tasks on Ubuntu using Cronjobs
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.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- An SSH key
- An Instance running Ubuntu Focal Fossa (20.04)
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.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
Understanding 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 |
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 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 * * * * . |
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.
-
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 <---- easiest 2. /usr/bin/vim.basic 3. /usr/bin/vim.tiny 4. /bin/ed Choose 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.
# 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
andO
to save your modifications, thenCTRL
andX
to quit the 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.
Visit our Help Center and find the answers to your most frequent questions.
Visit Help Center