凌的博客

您现在的位置是: 首页 > 学无止境 > Linux > 

Linux

crontab定期执行命令详解

2015-06-25 Linux 876
一,什么是crond,crontab linux下面定期分为二部分,一部分是后台程序crond,一部分是crontab往crond输入指令的接口。 为什么要定期执行,因为有些事情我们是要定时执行的,这样可以节省不少人力,物力。

一,什么是crond,crontab

linux下面定期分为二部分,一部分是后台程序crond,一部分是crontab往crond输入指令的接口。

为什么要定期执行,因为有些事情我们是要定时执行的,这样可以节省不少人力,物力。例如:每个星期都要给注册的用户发送邮件,如果能定时执行的话,就不要有个人去做了,现在sns比较流行,里面有什么好友新鲜事,你做的事情,不是立马你的好友就能看到,也许要过一小时,或者几个小时才能看到,如果要人工去操作的话,就比较烦了。


二,crond启动

crond不同的系统下面启动不同,以archlinux为例

1,crond启动

[root@BlackGhost zhangy]# crond

2,crond开机启动

a,利用rc.conf

DAEMONS=(syslog-ng alsa hal net-profiles httpd !slim @mysqld !network !netfs crond)

b,利用rc.local

在文件的最后加上sudo crond

三,crontab命令详解

  1. [root@BlackGhost etc]# crontab -h 
  2. crontab V3.2 
  3. crontab file <opts>  replace crontab from file   //修改文件存放位置 
  4. crontab -    <opts>  replace crontab from stdin 
  5. crontab -u user      specify user //指定用户 
  6. crontab -l [user]    list crontab for user // 查看命令列表 
  7. crontab -e [user]    edit crontab for user //编辑列表 
  8. crontab -d [user]    delete crontab for user //删除列表 
  9. crontab -c dir       specify crontab directory //指定crontab 目录 
  1. [root@BlackGhost etc]# crontab -h  
  2. crontab V3.2  
  3. crontab file <opts>  replace crontab from file   //修改文件存放位置  
  4. crontab -    <opts>  replace crontab from stdin  
  5. crontab -u user      specify user //指定用户  
  6. crontab -l [user]    list crontab for user // 查看命令列表  
  7. crontab -e [user]    edit crontab for user //编辑列表  
  8. crontab -d [user]    delete crontab for user //删除列表  
  9. crontab -c dir       specify crontab directory //指定crontab 目录  

四,crontab命令详解

1,crontab file 指定命令存放

archlinux下面默认crontab命令存放在是在/var/spool/cron/下面,以用户名来命名文件的

[root@BlackGhost etc]# ls /var/spool/cron
root  zhangy

如果我想换个位置怎么办呢,看下面

  1. [root@BlackGhost etc]# cp /var/spool/cron/root /tmp/root 
  2. [root@BlackGhost etc]# crontab /tmp/root    //修改文件存放位置 
  3. [root@BlackGhost etc]# crontab -l 
  4. # DO NOT EDIT THIS FILE MANUALLY!! USE crontab -e INSTEAD. 
  5.  
  6. # <minute> <hour> <day> <month> <dow> <command> 
  7.  
  8. 01 * * * *  /usr/sbin/run-cron /etc/cron.hourly 
  9. 02 00 * * * /usr/sbin/run-cron /etc/cron.daily 
  10. 22 00 * * 0 /usr/sbin/run-cron /etc/cron.weekly 
  11. 42 00 1 * * /usr/sbin/run-cron /etc/cron.monthly 
  12. #30 00 * * * /sbin/shutdown -h now 
  13. 00 18 * * * /home/zhangy/database_bak.sh 
  1. [root@BlackGhost etc]# cp /var/spool/cron/root /tmp/root  
  2. [root@BlackGhost etc]# crontab /tmp/root    //修改文件存放位置  
  3. [root@BlackGhost etc]# crontab -l  
  4. #  
  5. # DO NOT EDIT THIS FILE MANUALLY!! USE crontab -e INSTEAD.  
  6. #  
  7.   
  8. # <minute> <hour> <day> <month> <dow> <command>  
  9.   
  10. 01 * * * *  /usr/sbin/run-cron /etc/cron.hourly  
  11. 02 00 * * * /usr/sbin/run-cron /etc/cron.daily  
  12. 22 00 * * 0 /usr/sbin/run-cron /etc/cron.weekly  
  13. 42 00 1 * * /usr/sbin/run-cron /etc/cron.monthly  
  14. #30 00 * * * /sbin/shutdown -h now  
  15. 00 18 * * * /home/zhangy/database_bak.sh  

2,crontab -u,crontab -l,crontab -e,crontab -d

  1. [root@BlackGhost etc]# crontab -l -u zhangy  //没有任务 
  2. [root@BlackGhost etc]# crontab -e -u zhangy  //添加任务 
  3. [root@BlackGhost etc]# crontab -l -u zhangy  //查看任务 
  4. 00 18 * * * /home/zhangy/database_bak.sh 
  5. [root@BlackGhost etc]# crontab -d -u zhangy  //删除任务用户 
  6. [root@BlackGhost etc]# crontab -l -u zhangy  //用户已删除 
  7. no crontab for zhangy 
  8. [root@BlackGhost etc]# crontab -e -u zhangy  //添加任务 
  9. [root@BlackGhost etc]# crontab -l -u zhangy  //查看 
  10. 00 18 * * * /home/zhangy/database_bak.sh 
  1. [root@BlackGhost etc]# crontab -l -u zhangy  //没有任务  
  2. [root@BlackGhost etc]# crontab -e -u zhangy  //添加任务  
  3. [root@BlackGhost etc]# crontab -l -u zhangy  //查看任务  
  4. 00 18 * * * /home/zhangy/database_bak.sh  
  5. [root@BlackGhost etc]# crontab -d -u zhangy  //删除任务用户  
  6. [root@BlackGhost etc]# crontab -l -u zhangy  //用户已删除  
  7. no crontab for zhangy  
  8. [root@BlackGhost etc]# crontab -e -u zhangy  //添加任务  
  9. [root@BlackGhost etc]# crontab -l -u zhangy  //查看  
  10. 00 18 * * * /home/zhangy/database_bak.sh  

3,crontab书写規则

<minute> <hour> <day> <month> <dow> <command>
第1列      第2列          3             4            5                 6

第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令

crontab书写規则的例子很多,以下从网上找的,偷懒一下,嘿嘿

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

文章评论

0条评论