Scheduling tasks on Linux with the cron service

Last updated on 06 Sep 2023, 13:10:55.
Category: Cloud servers

CentOS Cron Linux

Cron is an application that executes planned tasks at a defined time on Linux systems. The "crontab" (Cron Table) is a configuration file where these tasks can be added.

By working with a cron job (or scheduled task) you can execute recurring commands behind the scene's through the cron service, for example erasing cache folders, launching a backup script, synchronisation of files...

Crontab syntax

Each user has its own crontab, it can be used simply by invoking the crontab command:

Modifying a crontab: crontab -e
Displaying a crontab: crontab -l
Removing all crontab entries: crontab -r

A crontab file contains a line for every planned task. The rule always starts with the definition of the scheduled execution time.
The line may seem overwhelming at first, but its structure is quite convenient.
A crontab line has 6 "fields", separated by whitespace (one or more spaces or tabs).

+------------- minutes (0 - 59) 
¦ +-------------- hours (0 - 23)
¦ ¦ +--------------- day of month (1 - 31)
¦ ¦ ¦ +---------------- month (1 - 12)
¦ ¦ ¦ ¦ +----------------- weekday (0 - 6) (0 is sunday, 1 is monday, ..., 7 is also sunday!)
¦ ¦ ¦ ¦ ¦
¦ ¦ ¦ ¦ ¦
* * * * * /usr/bin/command

A correct entry can be:

04 11 * * 3 /usr/sbin/ntpdate pool.ntp.org 2>&1 >/dev/null

The rule above defines that once a week, on wednesday (3) at 11h04 am the command "/usr/sbin/ntpdate pool.ntp.org 2>&1 >/dev/null" needs to be executed.
The notation "2>&1 >/dev/null" at the end of the command ensures all output is not passed to the console (Linux output redirection).

Do you wish to temporarily unuse a line?
Simply comment out the line. This is easily done by adding "#" in front of the command line.

#04 11 * * 3 /usr/sbin/ntpdate pool.ntp.org 2>&1 >/dev/null

System cron jobs

Aside the user you can also add cron jobs for the system.
These use an additional field, to point out which user must be used to execute the cron command.
In our example we tell that the task will be executed by the 'root' user.
(The 'root' user has all permissions on the system! From a security viewpoint this is not the best solution, but in our example we use this user because it is always available on the system.)

04 11 * * 3 root /usr/sbin/ntpdate pool.ntp.org 2>&1 >/dev/null

File locations

The crontabs for the system can be set up at the following locations:

1. The crontab file

/etc/crontab

Above is the location for the crontab file of the system configuration.

2. In the cron.d folders

/etc/cron.d/

The location above is a map where separate crontab files may be created, they will be executed by the cron service.

3. In specific folders that define the execution time

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

These are predefined execution times. Take care when using these files, you do not need to define an execution time for your command.
Just add your Linux shell scripts in these folders, they will be executed at the defined time!

Whether these files or folders are available also depends on your Linux distribution.

The definition for these folders (user, time) can usually be found in /etc/crontab.


Related articles

Kinamo mail server settings (mail server cheat sheet)

The following article provides the basic settings and server names (domain names) for the Kinamo mail infrastructure. These settings allow...

Read more

Create an automatic SPAM filter in the Kinamo Webmail

This article explains how you may create a filter that will automatically place SPAM messages in a special folder, so...

Read more

Order an SSL certificate? What is an SSL certificate?

You need to order an SSL certificate? But what is an SSL certificate? And why is everyone saying that it...

Read more

Need extra help?

Were not all your questions answered?
Don't worry, we will be happy to help you via a support request!

Select your language

All languages: