网普技术论坛 网普科技  
» 游客:   网普主页 | 注册 | 登录 | 帮助
 

作者:
标题: Cron Help Guide 上一主题 | 下一主题
网普科技
网普管理员

网普科技人民公仆


积分 3080
发贴 2863
注册 2004-6-26
来自 网普科技
状态 离线
#1  Cron Help Guide

by Joey.
Created 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 linuxhelp.ca.

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.



天理路上甚宽,稍游心,胸中便觉广大宏朗;
人欲路上甚窄,才寄迹,眼前俱是荆棘泥涂。



网普科技,优质美国主机服务!
美国Linux主机,美国虚拟主机
支持PHP+MYSQL+cPanel+EMAIL
为用户负责,拒绝反动、赌博及色情内容! QQ:126818

发送QQ消息
2005-8-25 01:40 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部
茱莉娅
THE BODY SHOP美容顾问

茱莉娅美体小铺


积分 3080
发贴 2863
注册 2009-5-21
来自 茱莉娅美体小铺
状态 离线
#1  赞助商信息The body shop

茱莉娅美体小铺
茱莉娅美体小铺淘宝店
茱莉娅美体小铺


茱莉娅美体小铺淘宝店
2005-8-25 01:40 PM
查看资料  访问主页  发短消息  QQ   编辑帖子  引用回复 顶部


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转:  




Powered by Discuz! 2.5 © 2001-2005 Comsenz Technology Ltd.
Processed in 0.012152 second(s), 7 queries, Gzip enabled
------------------------------------------------------------------------------
本论坛属网普科技交流与技术支持论坛!
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!
美国主机, 美国虚拟主机, cPanel+PHP+Mysql+Ftp+Email+Zend+GD2+国际域名支持
技术支持 QQ: 126818 EMail & MSN: support[AT]netpu.net
[ 联系我们 ] - [ 网普科技 ]