News
Seven days of Black Friday madness — great deal from Serverspace!
Serverspace Black Friday
JH
Joe Harris
January 31, 2021
Updated June 7, 2023

How to Automate Regular Tasks with Cron on Ubuntu 20.04

Linux Maintenance Ubuntu

Using another OS?

Select the desired version or distribution.

Сron allows you to automatically run tasks and scripts at specific intervals. Therefore, it is a very popular server administration tool. Cron is present in all Linux distributions. In this tutorial, we will set up regular tasks using Cron in Ubuntu 20.04 and walk through its syntax.

Installing cron

Most likely cron is installed on your system. But if suddenly this is not the case, you can fix it with the command:

apt install cron

Add it to autostart and run:

systemctl enable --now cron

Simple cron configuration

The easiest way to create a regular task using cron is to put the script in one of the following folders:

  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.weekly
  • /etc/cron.monthly

The frequency of execution is indicated in the folder name. It is important to set execution rights and exclude dots from script names.

The cron logs are located here: /var/log/syslog. Here are the logs of other applications. Therefore, to see the cron logs, use:

grep CRON /var/log/syslog

Advanced cron configuration

To set up cron jobs, enter:

crontab -e

On the first launch, you will be prompted to select your preferred text editor. Specify your choice and you will see an explanation of the cron setting from the developers, after which you can enter your tasks.

Recording format - the following values are entered through a space:

  • minute - from 0 to 59
  • hour - from 0 to 23
  • day of month - from 1 to 31
  • month - from 1 to 12 OR jan, feb, mar, apr, etc.
  • day of week - from 0 to 6 (Sunday = 0 or 7) OR sun, mon, tue, wed, thu, fri, sat
  • command

Asterisk - for all values. Example:

10 20 * * * /var/script

For this entry, cron will run the script every day at 20:10.

Additional cron configuration options

Hyphen (-) allows you to specify a range of values. For example, execution on weekdays:

10 20 * * 1-5

Comma (,) allows you to specify multiple values. For example, the launch at 10 and 20 o'clock can be written as follows:

10 10,20 * * *

Slash (/) allows you to specify a value step. For example, run every 10 minutes:

*/10 * * * *

In addition, there are special expressions for simple creation of tasks: @reboot, @yearly or @annually, @monthly, @weekly, @daily, @hourly, @midnight. When applied, the frequency will correspond to their name. For example:

@midnight /var/script

To see all configured cron jobs, use the command:

crontab -l

Managing cron jobs of other users

You can view cron jobs created by other users if you have administrator rights (sudo) or after logging in with the root account:

crontab -u username -l

And even edit them:

crontab -u username -e

Managing the rights to create cron jobs

Initially, each user has permission to create cron jobs.
To prohibit this action for certain users, add their logins to the /etc/cron.deny file. Other users will have the right.

To prevent cron jobs from being created for all but some users, add those users to the /etc/cron.allow file.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 4
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300
We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.