2014/05/11 21:07
阅读数 131

Cron Help Guide by Joey.
Created on September 19th 2003.
Last updated on September 19th 2003.

Cron comes from the word chronos, the Greek word for time. Cron is a utility that can help with automating certain tasks in Linux. For example if you would like to create backups of certain files or directories each night while you are sleeping, you can use Cron to automate this.

Cron stores it's enteries in the crontab (cron table) file. This is generally located in your /etc directory. As well, each user on your system can have their own crontab which would be stored in /var/spool/cron/. To edit a users crontab entry, simply log into your system for that particular user and type crontab -e. The default editor for the 'crontab -e' command is vi. If you are not familiar with VI you can change the default editor by running the following command export VISUAL='editor'. Of course you must replace editor with your favorite text editor (nano, pico, joe etc). Or you could always learn how to use VI ;)

Below are 2 entries in my /etc/crontab file. The first one is used to back up my /etc directory nightly. The second entry is to run the Analog program to calculate the web server stats for

12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1
52 5 * * * root /usr/local/src/analog-5.32-lh/analog >> /dev/null 2>&1

Below is a table of what each field does.

Field Meaning
1 Minute (0-59)
2 Hour (2-24)
3 Day of month (1-31)
4 Month (1-12, Jan, Feb, etc)
5 Day of week (0-6) 0 = Sunday, 1 = Monday etc or Sun, Mon, etc)
6 User that the command will run as
7 Command to execute

So using one of our original examples:

12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1

This will run tar czvf /usr/local/backups/daily/etc.tar.gz /etc at 3:12am every day. The >> /dev/null 2>&1 part means to send any standard output to /dev/null (the linux trash can) and to redirect standard error (2) to the same place as the standard output (1). Basically it runs the command without any output to a terminal etc.

Another example of a more complex entry would be:

30 15 13 6 1 * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1

This will run tar czvf /usr/local/backups/daily/etc.tar.gz /etc on Monday June 13th at 3:30pm
You could also use the following to achieve the same results

30 15 13 Jun Mon * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1

If you wanted to run a command as user joey 15 minutes after every hour regardless of the date you could add the following entry:

15 * * * * joey /usr/bin/somecommand >> /dev/null 2>&1

The astrix '*' in the example above is a wildcard meaning that cron will ignore the field.

If you wanted to run a command every 2 hours you could enter in */2 for the hour field. This would run the specified command at 2am, 4am, 6am, 8am, 10am, 12pm, 2pm, and so on. An example of this type of entry would be:

0 */2 * * * joey /usr/bin/somecommand >> /dev/null 2>&1

You can also use commas to specify more than one time per entry. For instance if you wanted to run a command at 15 and 30 past each hour you would enter in 15,30 for the minute field. An example of this type of entry would be:

15,30 * * * * joey /usr/bin/somecommand >> /dev/null 2>&1

If you wanted to run a command every day at a certain time for the first week of the month you would enter in 1-7 for the day field. An example of this type of entry would be:

15,30 */2 1-7 * * joey /usr/bin/somecommand >> /dev/null 2>&1

This would run somecommand every 2 hours at the 15's and 30's (2:15, 2:30, 4:15, 4:30 etc) for the first 7 days of the month.

If you want cron to execute a bunch of scripts at 4:18pm every day you could put all of the scripts in one directory (for example, /home/username/cron) and add the following line to your crontab:

18 16 * * * root run-parts /home/username/cron >> /dev/null 2>&1

If you wanted to save the output of a certain command you can replace the >> /dev/null 2>&1 with >> /home/user/somecommand.log 2>&1

After you've added all your entries you can use the command crontab -l to list them.

If you wanted to remove your crontab file you could run crontab -r to delete it.

To edit a users crontab file as root you can run crontab -e -u username

So as you can see, Cron is very configurable and is a great tool for every system administrator to automate tasks.


0 收藏
0 评论
0 收藏