Linux crond定时任务的分析与总结

  crond 是Linux用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。crond 命令每分钟(分钟级)定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。

一、Linux上crond调度任务分为系统级和用户级:
    1.系统执行的工作:系统周期性所要执行的工作,如备份系统数据、清理缓存 
    2.个人执行的工作:某个用户定期要做的工作,例如每隔10分钟检查邮件服务器是否有新信,这些工作可由每个用户自行设置

二、crond服务的开启关闭重启重新载入配置
    /sbin/service crond start   //启动服务 
    /sbin/service crond stop    //关闭服务 
    /sbin/service crond restart //重启服务 
    /sbin/service crond reload  //重新载入配置

三、crond其使用者的权限记载在下列两个文件中:
    /etc/cron.deny            该文件中所列的用户不允许使用Crontab命令
    /etc/cron.allow           该文件中所列的用户允许使用Crontab命令
    /var/spool/cron/          是所有用户的crontab文件存放目录(文件与用户名一致)

###系统crontab配置文件、目录型的的定时任务cron.hourly、cron.daily、cron.monthly、
###cron.weekly和cron.deny、cron.allow权限设置  
[root@localhost /]# ll /etc |grep cron  
-rw-r--r--  1 root root     298 2007-03-28 anacrontab  
drwx------  2 root root    4096 2012-02-23 cron.d  
drwxr-xr-x  2 root root    4096 03-26 05:05 cron.daily  
-rw-r--r--  1 root root       0 03-26 05:04 cron.deny  
drwxr-xr-x  2 root root    4096 2007-01-06 cron.hourly  
drwxr-xr-x  2 root root    4096 03-26 05:00 cron.monthly  
-rw-r--r--  1 root root     417 04-03 11:26 crontab  
drwxr-xr-x  2 root root    4096 03-26 05:05 cron.weekly  
[root@localhost /]# 

四、时间参数介绍
  第一段   分钟  0-59
  第二段  小时   0-23
  第三段  日期  1-31
  第四段  月份  1-12
  第五段  星期  0-6 (0代表星期天)
  除了数字还有几个个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"*/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。

五、用户级的定时任务设置:crontab (/usr/sbin/crontab)
     1.用户的限制文件(ron.all优先于cron.deny):
       /etc/cron.deny            该文件中所列的用户不允许使用Crontab命令
       /etc/cron.allow           该文件中所列的用户允许使用Crontab命令
     2.用户使用crontab建立工作任务后,工作会被记录在/var/spool/cron目录下
     3.crontab [-u username] [-e|l|r]
        参数:-e:编辑crontab的工作内容
              -l:查看crontab的工作内容
              -r:删除crontab的工作内容
              -u:设置某个用户的crontab服务(root用户)

##编辑添加工作内容  
[root@localhost /]# crontab -e  
crontab: installing new crontab  
You have mail in /var/spool/mail/root  
 
###添加工作内容,实际就是写到/var/spool/cron目录用户各自的文件内  
[root@localhost /]# ll /var/spool/cron  
total 4  
-rw------- 1 root root 41 04-03 16:33 root  
[root@localhost /]# cat /var/spool/cron/root  
* * * * *  date >>/usr/local/Tcron/1.txt  
 
###每分钟执行一次脚本  
###-l查看工作内容  
[root@localhost /]# crontab -l  
* * * * *  date >>/usr/local/Tcron/1.txt  
[root@localhost /]# cat /usr/local/Tcron/1.txt  
Wed Apr  3 16:30:01 CST 2013  
[root@localhost /]# cat /usr/local/Tcron/1.txt  
Wed Apr  3 16:30:01 CST 2013  
Wed Apr  3 16:31:01 CST 2013  
 
###-r删除工作内容  
[root@localhost /]# crontab -l  
* * * * *  date >>/usr/local/Tcron/1.txt  
[root@localhost /]# crontab -r  
[root@localhost /]# crontab -l  
no crontab for root  
[root@localhost /]#  
 
###/ect/cron.deny禁止用户设置,默认都允许  
[root@localhost /]# !vim  
vim /etc/cron.deny   
[root@localhost ~]# cat /etc/cron.deny  
tCron  
[root@localhost /]# su -l tCron  
[I have no name!@localhost ~]$ crontab -e  
You (tCron) are not allowed to use this program (crontab)  
See crontab(1) for more information  
 
###allow优先于deny  
[root@localhost ~]# cat /etc/cron.allow  
tCron  
[root@localhost ~]# cat /etc/cron.deny  
tCron  
[root@localhost ~]# su -l tCron  
[I have no name!@localhost ~]$ crontab -e  
no crontab for tCron - using an empty one  
crontab: no changes made to crontab

六、系统级的定时任务设置:/etc/crontab
     crond服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用cron服务做一些事情。用crontab配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。

###确认没有用户级的crontab文件设置,添加系统级的定时任务  
###添加了系统级的定时任务若没有生效,可执行重新加载配置或者重启crond服务  
[root@localhost ~]# crontab -l  
no crontab for root  
[root@localhost ~]# ll /usr/local/Tcron  
total 0  
[root@localhost ~]# cat /etc/crontab  
SHELL=/bin/bash  
#输入执行文件的搜索路径  
PATH=/sbin:/bin:/usr/sbin:/usr/bin  
#当例行命令发生错误时或执行结果有STDOUT/STDERR时,将错误消息或屏幕显示的消息传给谁  
MAILTO=root         
HOME=/  
 
# run-parts  
#每时的01分执行/etc/cron.hourly目录下的脚本
01 * * * *     root       run-parts /etc/cron.hourly   
#每日的4:01执行/etc/cron.daily目录下的脚本  
02 4 * * *     root       run-parts /etc/cron.daily     
#每周日的4:22执行/etc/cron.weekly目录下的脚本  
22 4 * * 0     root       run-parts /etc/cron.weekly   
#每月1号的4:42执行/etc/cron.monthly目录下的脚本
42 4 1 * *     root       run-parts /etc/cron.monthly  
###一定要指定用户,否则执行不成功
#每分钟执行一次写日期到/usr/local/Tcron/1.txt文件中
* * * * *  root  date >>/usr/local/Tcron/1.txt    
   
[root@localhost Tcron]# ll /usr/local/Tcron
-rw-r--r-- 1 root root 87 04-03 17:16 1.txt  
[root@localhost Tcron]# cat /usr/local/Tcron/1.txt  
Wed Apr  3 17:14:01 CST 2013  
Wed Apr  3 17:15:01 CST 2013  
Wed Apr  3 17:16:01 CST 2013  
[root@localhost Tcron]#  
 
###crond是基于分钟级的定时任务,那么假如要进行秒级的定时任务?
###可以按照下面两种方法操作从而实现秒级定时任务的实现  
 
###①通过运行脚本(用户级与系统级一样)进行秒级任务执行  
[root@localhost Tcron]# crontab -l  
no crontab for root  
[root@localhost Tcron]# ll  
total 4  
-rw-r--r-- 1 root root 74 04-03 17:29 1.sh  
[root@localhost Tcron]# chmod +x 1.sh  
[root@localhost Tcron]# pwd  
/usr/local/Tcron  
[root@localhost Tcron]# cat 1.sh  
#!/bin/bash  
 
while true;do  
date >>/usr/local/Tcron/1.txt 
#20秒 
sleep 20  
done  
[root@localhost Tcron]# cat /etc/crontab  
SHELL=/bin/bash  
PATH=/sbin:/bin:/usr/sbin:/usr/bin  
MAILTO=root  
HOME=/  
 
# run-parts  
01 * * * *     root       run-parts /etc/cron.hourly  
02 4 * * *     root       run-parts /etc/cron.daily  
22 4 * * 0     root       run-parts /etc/cron.weekly  
42 4 1 * *     root       run-parts /etc/cron.monthly  
*  * * * *     root       /usr/local/Tcron/1.sh  
 
[root@localhost Tcron]# ll  
total 8  
-rwxr-xr-x 1 root root  74 04-03 17:29 1.sh  
-rw-r--r-- 1 root root 348 04-03 17:33 1.txt  
[root@localhost Tcron]# cat 1.txt  
Wed Apr  3 19:57:01 CST 2013  
Wed Apr  3 19:57:21 CST 2013  
Wed Apr  3 19:57:41 CST 2013  
Wed Apr  3 19:58:01 CST 2013  
Wed Apr  3 19:58:01 CST 2013  
Wed Apr  3 19:58:21 CST 2013  
Wed Apr  3 19:58:21 CST 2013  
↑↑ 这个地方存在疑问:同一时间会输出多次数据,甚至出现时间差的问题
 
 
###②在crontab文件内控制(用户级与系统级一样),进行秒级任务执行  
[root@localhost Tcron]# cat /etc/crontab  
SHELL=/bin/bash  
PATH=/sbin:/bin:/usr/sbin:/usr/bin  
MAILTO=root  
HOME=/  
 
# run-parts  
01 * * * * root run-parts /etc/cron.hourly  
02 4 * * * root run-parts /etc/cron.daily  
22 4 * * 0 root run-parts /etc/cron.weekly  
42 4 1 * * root run-parts /etc/cron.monthly  
* 3 * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1  
* 3 * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1 
#20秒 
* * * * * root  sleep 20; date>>/usr/local/Tcron/1.txt  
-rwxr-xr-x 1 root root 70 04-03 19:55 1.sh  
[root@localhost Tcron]# ll  
total 8  
-rwxr-xr-x 1 root root  70 04-03 19:55 1.sh  
-rw-r--r-- 1 root root 116 04-03 20:01 1.txt  
[root@localhost Tcron]# cat 1.txt  
Wed Apr  3 20:01:21 CST 2013  
Wed Apr  3 20:01:21 CST 2013  
Wed Apr  3 20:01:21 CST 2013  
Wed Apr  3 20:01:21 CST 2013  
Wed Apr  3 20:01:41 CST 2013  
Wed Apr  3 20:01:41 CST 2013  
Wed Apr  3 20:01:41 CST 2013  
Wed Apr  3 20:02:01 CST 2013  
Wed Apr  3 20:02:01 CST 2013  
Wed Apr  3 20:02:01 CST 2013    
↑↑ 这个地方存在疑问:同一时间会输出多次数据,甚至出现时间差的问题

七、网络上查找的一些样例

### 以下为借鉴网上的一些例子,便于理解时间参数  
30 21 * * * /usr/local/etc/rc.d/lighttpd restart  
上面的例子表示每晚的21:30重启lighttpd 。  
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart  
上面的例子表示每月1、10、22日的4 : 45重启lighttpd 。  
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart  
上面的例子表示每周六、周日的1 : 10重启lighttpd 。  
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart  
上面的例子表示在每天18 : 00至23 : 00之间每隔30分钟重启lighttpd 。  
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart  
上面的例子表示每星期六的11 : 00 pm重启lighttpd 。  
* */1 * * * /usr/local/etc/rc.d/lighttpd restart  
每一小时重启lighttpd  
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart  
晚上11点到早上7点之间,每隔一小时重启lighttpd  
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart  
每月的4号与每周一到周三的11点重启lighttpd  
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart  
一月一号的4点重启lighttpd 
posted @ 2013-08-12 23:59  木子吾雨  阅读(1145)  评论(0)    收藏  举报