Table of Contents

How To Setup Cron Job In Linux Part1

Cron software utility, which derives it’s name from the Greek god of time, called Kronos, is as the name suggests, a time-based job scheduler used for various tasks (called cron jobs) that are to be executed automatically, at a time defined by the user.

It exists on all Linux distributions and other UNIX-like operating systems practically from their beginning, and it went through a lot of changes until it became what we know of it today.

Since every user of the operating system must have the ability to schedule various tasks in his own rights, this is accomplished with the help of crontab tables, which contain the time of execution and the path to the executable script.

As everywhere else in Linux, we can tighten the security by allowing or disallowing certain users to use the Cron utility. This is done by creating a cron.allow or cron.deny file in /etc directory, adding just one user per line there. If we add user john to cron.allow file, Cron will forbid all other users from using the crontab command. Same goes in other way, adding john to cron.deny will only forbid john from using it.

Adding a new scheduled task, or a new cron job, can be done by envoking the crontab -e command, which at the first use, will ask the user what’s his favorite text editor. Upon choosing one, user can continue adding a new line at the bottom of his crontab file. Each line in this file represents one scheduled task - or one cron job.

Once the user has added a task and saved the file, crontab will automatically be checked for accuracy, and if everything is set right, a new task will be scheduled. If there are errors, user will be noticed and asked to correct the mistakes in the crontab’s format.

Due to the relative complexity of the crontab form, Linux architects have developed another way of using the Cron daemon, and that is by using predefined directories in /etc directory, namely cron.hourly, cron.daily, cron.weekly and cron.monthly.

By simply placing our executable script inside any of these directories, they will be automatically executed by the run-parts script.

In production environments, this saves time and enables users to automate tasks without the need to dive into fine-tuning crontab files.

For those who like to look under the hood, let’s proceed with crontabs anatomy.

Format of a crontab file

Crontab consist of lines written in six columns, in which they specify time, date etc, and the path to the actual executable that we want to run.

  1. First column represents minutes, and can therefore contain values from 0 to 59;
  2. Second column represents hours, and can go from 0 to 23;
  3. Third column represents the day of a month, and can therefore go from 1 to 31;
  4. Fourth column contains months, from 1 to 12;
  5. Fifth column represents the day of a week, and can go from 0 to 7 (in Linux, Sunday can be both 0 and 7, depending on the system, and can also be written in a three letter short format, such as Sun, Mon, Tue, etc);
  6. Sixth column contains a path to the executable script;

As Wikipedia nicely presents:

# ┌───────────── minute (0 - 59)

# │ ┌───────────── hour (0 - 23)

# │ │ ┌───────────── day of the month (1 - 31)

# │ │ │ ┌───────────── month (1 - 12)

# │ │ │ │ ┌───────────── day of the week (0 - 7)

# │ │ │ │ │                                  

# │ │ │ │ │

# │ │ │ │ │

# * * * * * command to execute

Automation of various maintenance tasks Linux is so good at wouldn’t be possible without Cron. Let’s say you host a simple static website for example, and would like it to be backed up on a daily basis.

We can write a simple bash script, which will create time-stamped .tar.gz archive of all the contents in the www directory.

Open your favorite text editor, and paste this in, adjusting the highlighted paths according to your needs:

#!/bin/bash
tar -zcf /home/user/backups/"$(date '+%m-%d-%y').tar.gz" /home/www/

Save the script as website-backup.sh somewhere on the computer and then make it executable with the following command:

chmod +x /home/user/path-to-our-script/website-backup.sh

Once done, we have our executable script which we’ll run from our crontab.

Now, let’s assume we would like this script to run every night at midnight.

If so, we will invoke the crontab with: crontab -e and will add this to the end of the file:

0 0 * * * /home/user/path-to-our-script/website-backup.sh

Once saved, crontab will add our new cron job and we will have our backup created every night at midnight.

Now, if instead of every midnight, we would like this script to run once a week, on Tuesday at midnight, we would adjust our crontab like so:

0 0 * * 2 /home/user/path-to-our-script/website-backup.sh

Number 2, in the fifth column represents the second day of the week, Tuesday. Same result can be achieved by using Tue in the fifth column, like this:

0 0 * * Tue /home/user/path-to-our-script/website-backup.sh

Let us now think of a different scenario. Let’s say you would for example like this script to be run at November 18th, 2:27PM. We would then use:

27 14 18 11 * /home/user/path-to-our-script/website-backup.sh

It doesn’t stop there, we can complicate things much further, and use multiple values or even whole ranges for each of the columns, enabling us to run our backup for example every day, from November 18th to November 28th, by adjusting our crontab like this:

27 14 18-28 11 * /home/user/path-to-our-script/website-backup.sh

Or, we can tell Cron to take backups just on November 18th and November 23rd, by separating values with a comma, like this:

27 14 18,23 11 * /home/user/path-to-our-script/website-backup.sh

As for the sixth column, which contains a path to our executable, we can have more than one value there as well, we can add another script there and both will run at the same time. All we have to do is to append it to the same line, separating the two with a semicolon, like this:

27 14 18 11 * /home/user/path-to-our-script/website-backup.sh ; /home/user/path-to-our-script/other-script.sh

All in all, Cron is an excellent tool which enables us to automate all kinds of tasks, allowing us to deal with other things that matter. It is a vital part of every Linux Administrator’s arsenal of tools!

Related Posts