What is cron and cronjob

W

In UNIX, cron is a job scheduler that allows actions to be performed at a time. Actions are generally defined by scripts, and they are run by cron at regular time intervals. It is mainly used to manage the system, but it can also be used for general activities such as Internet connection or downloading emails. Each line in the crontab file is a cron expression followed by the shell command to be executed.

Cron is a process organizer that greatly facilitates our work. It helps us to run programs regularly at certain times of the day. It is generally used for the operating system maintenance, for backup or for updating programs and databases.

crond is the daemon running in the background of UNIX systems and of other similar systems, and which continuously checks the list of processes to run at a specific date/time. The main configuration file of it is /etc/crontab that specifies the programs to be executed at a specific time on a given date.
For example, if you open /etc/crontab with a simple text editor, you’ll probably find several lines, among them being some similar ones to the ones below:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

The first 4 rows set the variables that are used by the crond to run. These variables (except MAILTO) are found in any account on a UNIX system.
SHELL is the interpreter used by crond to run applications.
PATH is the variable that preserves the way you can find the application to be run. If the app is specified as an application, it will be searched in all directories in the PATH variable.
MAILTO must have an account on your local system or a mailing address. After running, some scripts may display text, error messages, or run confirmation. These exit texts are automatically mailed to the account specified in MAILTO.
HOME is the homedir of the user under which crond runs. Generally, this variable is not required, but there are some programs that require it, so it’s good for it to have a value (/).

If you look in the second part of the /etc/crontab file, you see a list of separate stuff (or TAB). In fact, there are seven different fields, each with a well-defined purpose. Each field must exist. The top 5 fields tell the daemon when a particular application needs to be run. The sixth field specifies the user who has to execute the respective application. The seventh field is the command to be executed.

As we have it clear now, let’s see what the first five-character group is about, the one we said it should be the date. Indeed, it is the date and the fields are organized as follows:

• the minute – the minute of the hour the application will run;
• the hour – time of the day when the application will be run;
• the day (of a month) – on which day of the month the application will be run;
• the month – the month in which the application will be run;
• the day (of the week) – on which day of the week the application will run.

As you can see, we have a fairly high flexibility in specifying the time when the application will run. We can specify each term in the 5 or just write an asterisk (*) to specify that the application should be run every minute/day/hour/month depending on the field in which we place it.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories