Why And What Is "Cron"?

In this article we are going to get acquainted with a Linux tool named "cron." As the name implies, cron (from the Greek chronos- "time") allows users to set time-based, automatic processes.

A process is considered automatic in 2 cases. The process is waiting for:

- given system resources to take place
- given a date and time to take place

Jobs created by cron are waiting for the given date and time to take place. When the system date and time matches the user-specified values, the job will run. Cron is a very good solution if you want to periodically run programs/scripts in your Linux machine. Some examples of when cron can be used:

Daily database backups
Long task scheduling over weekend
Sending daily emails
Getting some monitoring data every 10 minutes
Powering off machine at the end of every working day.

This is only a very short list of tasks that cron can schedule and run automatically. Cron's potential is much more extensive and, due to its usefulness, it has become a good assistance tool for linux system administrators.
Scheduling a cron job.

To write a cron job, type crontab command with –e flag in terminal: 

$ crontab -e

It will open a cron file(in your default editor) where we can input your cron job. As an example, our first job will let us turn off our machine every work day at 18:00.
Type the following line in file, save and exit.

0 18 * * 1-5 /usr/bin/poweroff

That's actually it! Now our machine will turn off every day (except Sunday and Saturday) at 18:00.
Please note that each user in the system has their own crontab file, so if you open crontab file with the sudo command

$ sudo crontab -e

the crontab file for the root user will be opened, and command added into that file will run with root user privileges.
To see what cron jobs we already have, we can just use crontab with –l option.

$ crontab -l

Now let’s examine the syntax for cron commands.
Cron has a very simple syntax. First 5 values are date and time, and the last one is the actual command that need to be executed.

1st value “0” is minutes.
2nd value “18” is hour.
3th value “*” is day of month (* means every day in month).
4th value “*” is month (* means every month).
5th value “1-5” is day of week (1-5 means Mon, Tue, Wed, Thu, Fri).

For each value one we can setone value such as (0, 18),
range of values such as (1-5)
or mark every acceptable value with an asterisk “*”
Here is an example

Also there is a special keyword to help us run a task on reboot. For example: to run a script every time your server starts you can enter something like this:

$ @reboot /usr/bin/my_script

Just a few more useful examples-
Run a cron task every minute

* * * * * [command]

Run a cron task every 5 minutes

0/5 * * * * [command]

Run a cron task at 02:00, 03:00, 04:00, 05:00

0 2-5 * * * [command]

That basically covers the basic functionality of how the cron commands work. Be sure to hit us up on Twitter or Facebook if you have any questions, and be sure to follow and like our pages to keep up with more guides, tips, news and updates!

March 18 2016

Add or review comments

Please leave your comment

Existing comments

Comments 0


Get notified about new publications and product updates.
Please note we do not share information to anyone.