Into the (open) source


Crontab: planned actions with Linux


This article was written in the 2013, there may have been some developments in the subject matter.
If in doubt please leave a comment in the bottom of the article.


The program we are discussing today is cron, a lightweight Linux daemon that deals with launch command at user defined intervals. It is often confused with "crontab", which in reality is nothing but its configuration file, containing the commands to launch and the time intervals. Someone will remember crontab (we used in the tutorial on "time lapse" to take pictures in automatic). In today's article we see an overview of working with cron, accompanied by examples of use, and modify the crontab file.

You can execute crontab if the user alias of those who try to change it appears in the file "/usr/lib/cron/cron.allow". If the file does not exist, you can use crontab if your name does not appear in the user file "/usr/lib/cron/cron.deny". In the case where there is only the file "cron.deny" and is empty, all users can use crontab. If neither file exists, only root can use crontab. The structure of the two files (allow e deny) consists of a user alias per line.

Crontab commands

export EDITOR=nano ;specify a editor to open crontab

Command Description
crontab -e Edit the crontab file, or create one if does not exist
crontab -l View the crontab file
crontab -r Removes the crontab file
crontab -v Shows the last time that you changed the files (only possible on some systems)

The syntax of crontab

A crontab file has five fields to specify day, ORA date on, followed by the command to be executed.

* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday = 0)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- now (0 - 23)
+------------- minute (0 - 59)

This schemino is also indicated in the crontab file and is self-explanatory: when we want to execute the command replace the asterisks (or the asterisk) with the numerical values ​​given.

For operating systems that support it, you can also specify "/2" to run the command every two minutes. If you try to use it and crontab complains it is probably not supported. To work around this obstacle in the, you can duplicate the line crontab and set a sleep for a few seconds.

* * * * * /home/user/script/
* * * * * sleep 15; /home/user/script/
* * * * * sleep 30; /home/user/script/
* * * * * sleep 45; /home/user/script/

In this example, the script will run every minute, the second with a delay of 15 seconds, the third of 30 and the fourth 45.

As we have seen, the specification of the day can be made on a weekly or monthly. If both fields are filled, the command will be executed in both situations.

A few examples

This line of crontab deletes the contents of the directory "/home/user/tmp/" every evening at 18.30.

30 18 * * * rm /home/user/tmp/*

By changing the parameters as below will cause a shift in command execution.

min. hours day / month month day / week when you start?
30 0 1 1,6,12 * 00.30 the 1 January, June and December
0 20 * 10 1-5 at 20.00 every day of the week (lun-ven) only in October
0 0 1,10,15 * * midnight, the 1st, the 10 and the 15 pulls the
5,10 0 10 * 1 at 12.05, 12.10 every Monday and 10 of each month


It is of course possible to make crontab log records of. In this case the command (take as an example the same as before) will thus formed:

30 18 * * * rm /home/user/tmp/* > /home/user/cronlogs/clean_tmp_dir.log


For those who are not familiar with the command line, or find it particularly laborious, there are several alternatives for editing crontab via web interface. The only one that I would recommend is webmin, as well as allowing editing crontab, allows the management of the whole system linux without having to set SSH access or use the terminal, you can do everything via web interface. But perhaps this program deserves a dedicated article.



I keep this blog as a hobby by 2009. I am passionate about graphic, technology, software Open Source. Among my articles will be easy to find music, and some personal thoughts, but I prefer the direct line of the blog mainly to technology. For more information contact me.