Cookie

Caution


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

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 Show 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.

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/script.sh
* * * * * sleep 15; /home/user/script/script.sh
* * * * * sleep 30; /home/user/script/script.sh
* * * * * sleep 45; /home/user/script/script.sh

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.

  Tilda: terminal disappearance

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. hours day / month month day / week when you start?
30 1 1,6,12 * 00.30 the 1 January, June and December
20 * 10 1-5 at 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,10 10 * 1 at 12.05, 12.10 every Monday and 10 of each month

Log

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

Alternative

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.

webmin_cron


TheJoe

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.

0 Comments

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.