Linux基础介绍【第六篇】

定时任务crond介绍

crond是什么?

crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件。一般情况下,安装完CentOS5/6 linux操作系统之后,默认便会启动crond任务调度服务。crond服务会定期(默认每分钟检查一次)检查系统中是否有要执行的任务工作。如果有便会根据其预先设定的定时任务规则自动执行该定时任务工作。

特殊需求crond服务不能满足要求,一般工作中写脚本守护程序执行。

为什么要使用crond定时任务?

例如:我们的数据库或者代码程序需要每天晚上0点做一次全备,这样每天夜里都需要执行的周期性的工作。这就是linux系统的定时任务crond。实时备份,定时备份。

linux系统crond的定时任务

linux系统中定时任务调度的工作可以分为以下两种情况:

1、linux系统自身定期执行的任务工作:系统周期性自行执行的任务工作,如轮询系统日志、备份系统数据、清理系统缓存等,这些任务无需我们人为干预。

  1. #系统的日志
  2. [root@oldboy ~]# ls -l /var/log/messages*
  3. -rw------- 1 root root 598574 Dec 27 09:33 /var/log/messages
  4. -rw-------. 1 root root 292975 Dec 24 19:11 /var/log/messages-20161224
  5. -rw------- 1 root root 80222 Dec 25 10:05 /var/log/messages-20161225
  6. #安全文件
  7. [root@oldboy ~]# ls -l /var/log/secure*
  8. -rw------- 1 root root 11027 Dec 27 09:06 /var/log/secure
  9. -rw-------. 1 root root 5899 Dec 24 18:29 /var/log/secure-20161224
  10. -rw------- 1 root root 826 Dec 25 09:02 /var/log/secure-20161225

系统自动轮询任务的设置配置路径

  1. [root@oldboy ~]# cd /etc/logrotate.d
  2. [root@oldboy logrotate.d]# cat syslog
  3. /var/log/cron
  4. /var/log/maillog
  5. /var/log/messages
  6. /var/log/secure
  7. /var/log/spooler
  8. {
  9.     sharedscripts
  10.     postrotate
  11.         /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
  12.     endscript
  13. }

部分公司可能会放在/etc/corntab。

  1. [root@oldboy ~]# cat /etc/crontab
  2. SHELL=/bin/bash
  3. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  4. MAILTO=root
  5. HOME=/
  6.  
  7. # For details see man 4 crontabs
  8.  
  9. # Example of job definition:
  10. # .---------------- minute (0 - 59)
  11. # | .------------- hour (0 - 23)
  12. # | | .---------- day of month (1 - 31)
  13. # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
  14. # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
  15. # | | | | |
  16. # * * * * * user-name command to be executed

crond相关文件

  1. [root@oldboy ~]# ls -l /etc/|grep cron
  2. -rw-------. 1 root root 541 Nov 23 2013 anacrontab
  3. drwxr-xr-x. 2 root root 4096 Dec 16 22:09 cron.d
  4. drwxr-xr-x. 2 root root 4096 Dec 16 22:10 cron.daily
  5. -rw-------. 1 root root 0 Nov 23 2013 cron.deny
  6. drwxr-xr-x. 2 root root 4096 Dec 16 22:08 cron.hourly
  7. drwxr-xr-x. 2 root root 4096 Dec 16 22:09 cron.monthly
  8. -rw-r--r--. 1 root root 457 Sep 27 2011 crontab
  9. drwxr-xr-x. 2 root root 4096 Sep 27 2011 cron.weekly

2、用户执行的任务工作,某个用户或系统管理员定期要做的任务工作,例如每隔5分钟和互联网上时间服务器进行时间同步,每天晚上0点备份网站站点数据及数据库数据,一般这些工作需要每个用户自行设置。

编辑定时任务:crontab -e

显示定时任务:crontab -l

  1. [root@oldboy ~]# crontab -l
  2. */5 * * * * /use/sbin/ntpdate time.nist.gov >/dev/null 2>&1

linux系统下定时任务软件种类

linux系统下的定时任务软件,例如at、crontab、anacron。

at:适合仅执行一次就结束的调度任务命令,例如:某天晚上需要处理一个任务,仅仅是这一天的晚上,属于突发性的工作任务。要执行at命令,还需要启动一个名为atd的服务才行。工作中很少用到。

  1. [root@oldboy ~]# chkconfig --list|grep atd
  2. atd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

crontab:可以周期性的执行任务工作,例如:每五分钟做一次服务器时间同步。要执行crontab这个命令,需要启动一个服务crond才行,这个crontab命令工作中最常使用。

  1. [root@oldboy ~]# chkconfig --list|grep crond
  2. crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off

anacron:这个命令主要用于非7*24小时开机的服务器准备的,anacron并不能指定具体时间执行任务工作,而是以天位周期或者在系统每次开机后执行的任务工作。它会检测服务器停机期间应该执行,但是并没有进行的任务工作,并将该任务执行一遍。

注意:

1、crond服务是运行的程序,而crontab命令是用户用来设置定时规则的命令。

2、crond服务是企业生产工作中最常用的重要服务,at和anacron很少使用,可以忽略。

3、几乎每个服务器都会用到crond服务。

crontab是特权命令

  1. [root@oldboy ~]# ls -l `which crontab`
  2. -rwsr-xr-x. 1 root root 51784 Nov 23 2013 /usr/bin/crontab

crontab语法格式中特殊符号的含义

特殊符号

含义

*

*号,表示任意时间,就是"每"的意思。例如:00 23 * * * cmd表示每月每周每日的23:00都执行cmd任务。需要注意的是,每个时间位上的*表达每,如果位上是*就是该位上时间的取值范围,例如:小时上的*等价于00-23。

-

减号,表示分隔符,表示一个时间范围,区间段,如17-19点,每天的17,18,19点的00分执行任务。00 17-19 * * * cmd。就是17,18,19点整点分别执行的意思。

,

逗号,表示分隔时段的意思。30 17,18,19 * * * /bin/sh /scripts/oldboy.sh表示每天17,18,19点的半点时刻执行/scripts/oldboy.sh脚本。也可以和"-"结合使用,例如:30 3-5,17-19 * * * /bin/sh /scripts/oldboy.sh。

/n

n代表数字,即"每隔n单位时间",例如:每10分钟执行一次任务可以写成*/10 * * * * cmd,其中*/10中的*的范围是0-59,因此也可以写成0-59/10 * * * * cmd。

书写crontab定时任务

每分钟答应打印一次自己的名字拼音全拼到/server/log/自己的名字命名的文件。

  1. [root@oldboy oldboy]# crontab -e
  2. #每分钟打印自己的名字到/test/oldboy/yinshoucheng.txt
  3. * * * * * echo 'yinshoucheng' >> /test/oldboy/yinshoucheng.txt
  4. [root@oldboy oldboy]# tail -f /test/oldboy/yinshoucheng.txt

小结:

1、定时任务要加注释

2、结尾不要有>/dev/null 2>&1

3、定向的目录必须要存在

4、定时任务中的路径一定要绝对路径

5、crond服务必须开启运行

工作中调试定时任务的方法:

1、增加执行任务频率调试任务。

代码发布:个人开发环境à办公测试环境àIDC机房测试环境àIDC正式环境(分组、灰度发布)

2、调整系统时间调试任务(不能直接用于生产环境),保持5分钟。

3、通过脚本日志输出调试定时任务。

4、注意一些任务命令带来的问题。(>/dev/null 2>&1)

5、注意环境变量导致的定时任务故障。

6、通过crond定时任务服务日志调试定时任务(/var/log/cron)。

crontab定时任务生产注意的问题:

1、系统环境变量问题。

2、定时任务要用绝对路径。

3、脚本权限问题加/bin/sh。

4、时间变量问题用反斜线\%转义,最好用脚本。

5、>/dev/null 2>&1问题(1>/dev/null 2>/dev/null,&>/dev/null)。

6、定时任务规则之前加注释。

7、使用脚本程序替代命令行定时任务。

8、避免不必要的程序及命令输出(tar不使用v)。

9、切到目标目录的上一级打包目标。

10、定时任务脚本中的程序命令尽量用全路径(和环境变量的识别有关)。

原文地址:https://www.cnblogs.com/yinshoucheng-golden/p/6233869.html