Last updated on 06 Sep 2023, 13:10:55.
Category: Cloud servers
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...
Each user has its own crontab, it can be used simply by invoking the
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
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
The crontabs for the system can be set up at the following locations:
1. The crontab file
Above is the location for the crontab file of the system configuration.
2. In the cron.d folders
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.
Were not all your questions answered?
Don't worry, we will be happy to help you via a support request!