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

Managed services in the spotlight

Our system administrators have lifted a couple of cargo ships filled with containers of work. (yes, that pun was on...

Read more

What service level guarantees (SLA) are available for a server housing (co-location)?

Server housing or co-location customers can fall back on an excellent service level guarantee. A Kinamo server housing service comes...

Read more

Discover here how we’ve fixed a blocking error when upgrading ESXi 6.5 to 7.0 update 2 through vSphere Lifecycle Manager.

Kinamo maintains multiple VMware vSphere clusters, not only for our own infrastructure, but also for a variety of customers. As...

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!

Kinamo

Select your language

All languages: