This article was published more than a year ago, there may have been developments.
Please take this into account.

The program we are discussing today is cron, a lightweight Linux daemon which is responsible for launching commands 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 to 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 run crontab if the alias user who tries to modify 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

crontab -eEdit the crontab file, or create one if does not exist
crontab -lView the crontab file
crontab -rRemoves the crontab file
crontab -vShow the last time 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 asterisk) with the numerical values ​​given.

  Real HDR effect with Qtpfsgui

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 the execution of the command.

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


It is of course possible to make crontab log records of activity. 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 one that I would recommend is webmin, as well as allowing editing crontab, It allows the entire linux system administration 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.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.