Cron is a utility that manages background processes or daemons from time to time (for example each minute, day, week or month). Cron works like a scheduler, for example, it can download files from the internet or download emails at regular intervals, also you can use it for more complex tasks like install updates. Originally cron was created for UNIX by Ken Thompson, but now you can use it in all UNIX-like systems, even Linux! UNIX have other utility for tasks scheduling called “at” but cron is much better.
There are two ways to use cron, the first is through the cron directories located in /etc if you open a terminal and type the following command:
$ ls /etc/cron*
You will get the following output:
Now, you can see the following directories in the image: cron.daily, cron.hourly, cron.monthly, cron.weekly and cron.yearly.
These directories store the scripts that must be executed by cron, according to the name of each directory is the frequency of the execution. For example, the scripts stored in cron.hourly will be executed each hour.
Now, I’m going to write a simple script to make a backup of a directory, and I’ll put it on the cron.hourly directory to execute the script each hour. One important thing that you must consider, is to provide the execution permissions to the script, ‘cause if you don’t, cron won’t execute the file.
The example of the process to add a script to cron tasks using the global cron directories:
I have a directory and I want to make and automatically backup each hour:
I write a script to make the backup, I used nano but you can use your favorite editor:
I test the script:
I copy the script to /etc/cron.hourly:
I give the execution permissions to the script:
All the scripts must start with the line #!/bin/sh because it indicates the shell to use.
Use cron through the /etc/cron.* directories aren't very useful, so I’m going to explain the second-way using crontab.
If you remember the output when I listed the content of /etc/cron* you’ll see the following file: /etc/crontab
You can schedule tasks in the file crontab, but first, you must learn the following:
If you analyze the content of crontab file, you can identify 7 fields in the lines after “# run-parts”, I mean the following ( the first line after “# run parts”):
The first five fields indicate time values, the sixth field indicates the user and the seventh indicates the command to execute, this last can be a script or a command (uname, ls, apt-get, etc).
Time fields are following:
The accepted value for each field is:
If the value of one of these fields is “*”, it means all possibles values for the correspondent field, for example, * * * * * root /root/script.sh
This task will be executed by root every minute, all days and all months.
Crontab uses too special strings to define ranges of frequency:
You just change the time fields for one of the special strings and then complete the rest, for example, @annually root dist-upgrade.
This task should be executed each year automatically.
Now you can add tasks to crontab, also you can make a .scripts directory at your /home and store your scripts in it, finally, you can add an entry for every script in the crontab file.
Crontab can manage a tasks file for each user, you can test if your user has crontab tasks using the following command:
You can schedule tasks in a file, just create it:
When you have been created your crontab file, you must load it to cron typing the command:
$ crontab your_crontab
Cron is very powerful because after that you loaded a new crontab, you don't have to do anything, that’s all, cron load new tasks and crontab files automatically.
The result of my crontab is a script that makes a file with dates generated every minute:
Other options for crontab are the following:
Cron is the most powerful task scheduler that I know and it’s excellent because it isn’t just for Linux, it’s for all UNIX-like systems. If you manage a server, probably you’ll love cron, even if you’re a common user you’ll like cron, with cron you can schedule everything.
If you want to know more about cron, you should see the man pages:
$ man cron
$ man crontab
Most Read Articles
Translate Our Site