Cron is a daemon that can be used to schedule the execution of recurring jobs according to a combination of the time, day of the month, month, day of the week, and week.

The cron daemon "crond" running in the background is responsible for launching jobs at the scheduled time. Cron assumes that the system is on continuously. If the system is not on when a job is scheduled, it is not executed.

Cron jobs are used for the scheduling of tasks to run on the server and for automating system maintenance. Examples of such tasks include perform periodic backups, monitor the system, run custom scripts, and synchronization of files.

The cron daemon is automatically installed on most Linux distributions and entered into the startup scripts. To use it the daemon, the crond service must be running. To check if crond is running, type the following as root user:

systemctl status crond

Configuring Cron Jobs
The configuration file for cron jobs is saved is stored in /etc/crontab directory which can be only modified by the root user. To view, type the command below:

cat /etc/crontab

From the above image, the first three lines are variables used to configure the environment in which the cron jobs are run.

  • The SHELL variable tells the system which shell environment to use (in this example the bash shell)
  • The PATH variable defines the path used to execute commands.
  • The MAILTO variable contains the username defined where the output of the cron jobs are emailed to.

To edit your crontab, type the command below and a text editor will be shown where you can input your 1
schedule with each job on a new line.

crontab -e

To view your crontab, type the command below:

crontab -l

To erase your crontab, type the command below:

crontab -r

The following is the format for each line in the /etc/crontab file which represents a job:

minute   hour   day   month   dayofweek   command
  1. minute — any integer from 0 to 59
  2. hour — any integer from 0 to 23
  3. day — any integer from 1 to 31 (must be a valid day if a month is specified)
  4. month — any integer from 1 to 12 (or the short name of the month such as jan or feb)
  5. dayofweek — any integer from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as sun or mon)
  6. command — the command to execute.

An asterisk (*) can also be used on any or all field to indicate that at all Hours, minutes, etc. should the command be executed. For example, to run the backup script in the /home/user/ directory every 10 minutes, the command is shown below:

10 * * * * /home/user/backup.sh

You can also execute tasks hourly, weekly, monthly or daily by inserting your script in any of these directories: /etc/cron.daily/, /etc/cron.hourly/, /etc/cron.monthly/, /etc/cron.weekly/.

Blocking Access
You can also block access to cron with the /etc/cron.allow and /etc/cron.deny files. By default, most cron assumes all users have access to cron unless one of these file exists. For example, to grant access to a user (ayo in this case) but deny all users, type the command below:

echo ALL >>/etc/cron.deny
echo ayo >>/etc/cron.allow

You can also check the man pages of crontab and crond for more information.