首页 > 技术文章 > 定时任务

yjiu1990 2019-01-18 17:09 原文

## 定时任务分类
#### 定时任务分为三类
    crond:定时任务软件(cronie软件包)
    atd:只运行一次
    anacron:非7*24运行的服务器
## 用户定时与系统定时任务
#### 系统定时任务文件:如系统中毒后需清理以下文件内容
    /etc/cron.hourly:系统每小时运行的程序
    /etc/cron.daily:系统每天运行的程序
    /etc/cron.weekly:系统每周运行的程序
    /etc/cron.monthly:系统每运行的程序
    /etc/cron.deny:定时任务黑名单
    /etc/crontab 定时任务配置之一
    /系统定时任务+logrotate命令完成日志的日志切割与日志询
#### 用户定时任务
    crontab -l:查看用户的定时任务
    crontab -e:编辑用户的定时任务
    /var/spool/cron/root(root用户的定时任务)
#### 查看crond服务是否运行
        方法一:
        [root@web02 ~]# /etc/init.d/crond status
        crond (pid  1581) is running...
        方法二:
        [root@web02 ~]# ps -ef |grep crond
        root       1581      1  0 15:58 ?        00:00:01 crond
        root       2175   2024  0 23:27 pts/1    00:00:00 grep --color=auto crond
        方法三:是否开机运行
        [root@web02 scrpits]# chkconfig |grep crond
        crond 0:off    1:off    2:on    3:on    4:on    5:on    6:off

#### 定时任务的常见写法
```
  # Example of job definition:
  # .---------------- minute (0 - 59)
  # | .------------- hour (0 - 23)
  # | | .---------- day of month (1 - 31)
  # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
  # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
  # | | | | |
  # *  *  *  *  *  command to be executed
    分 时 日 月 周 命令
```
##### 定时任务中的特殊符号
    *:每,如* * * * * 命令
    /n:每隔,如*/5 * * * * 每隔五分钟运行一次
    -:从哪里到哪里,如07-08,每天上午的7点到8点
    ,:分隔,如00 07,09,11 * * * ,每天7点9点11点整点运行一次
    注:如想表示整点,分钟必须写上00,如:* 07-11 * * * 表示的是每天7点到11点之前每分钟运行一次,00 07-11 * * *表示的是每天7点到11点整每个小时运行一次
#### 定时任务的文件说明
| 文件 | 说明 |
| --- | --- |
| /etc/cron.deny | 该文件列出的用户不能使用crontab命令 |
| /etc/cron.allow | 该文件列出的用户允许使用crontab命令,优先于/etc/cron.deny |
| /var/spool/cron | 所有用户crontab配置文件默认都存放在此目录,文件名以用户名命名。1. 定时任务的配置文件,定时任务规则2. 文件名就是用户名(root) |
#### crontab命令
| 参数 | 含义 | 指定示例 |
| --- | --- | --- |
| -l(小写L) | 查看crontab文件内容 | crontab -l |
| -e | 编辑crontab文件内容 | crontab -e |
| -i | 删除crontab文件内容,删除前会提示确认 | crontab -ri |
| -r | 删除crontab文件内容 | crontab -r |
| -u user | 指定使用的用户执行任务。 | crontab -u luffy -l |

强调:-i -r基本不用。尽量只用-e
crontab -e 实际操作/var/spool/cron里的文件(以用户名命名)
crontab优点:
1.  检查语法
2.  输入方便

#### 定时任务的八大要领
    要领1:为定时任务罪责加必要的注释
    要领2:执行shell脚本任务前加/bin/sh
    要领3:定时任务命令或脚本的结尾加 >/dev/null(表示黑洞) 2>&1(正确或错误都输出到文件)
    要领4:定时任务命令或程序最好写到脚本里执行 
    要领5:在指定用户下执行相关定时任务 
    要领6:生产任务程序不要随意打印输出信息 tar zcf 
    要领7:定时任务执行的脚本要规范路径(全路径) 
    要领8:配置定时任务规范操作过程,防止出错

#### 定时任务的书写流程
    1.先在命令行中使用命令测试
    2.将命令放入脚本中
    3.测试脚本是否可以使用
    4.写定时任务:(先测试每分钟执行,测试无问题后改为要求的时间)
    5.测试结果:1.查看定时任务日志2.查看命令是否执行成功
#### 定时任务PATH环境变量问题
##### 定时任务运行脚本只识别/bin,/usr/bin,找不到路径会报command not found
    解决方法:
    1.命令使用绝对路径,如ifconfig的绝对路径是/sbin/ifconfig
    2.在脚本开头重新定义一下PATH:如export  PATH=/usr/local/python3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:

#### 例子1:系统每五分钟更新一下时间
    [root@web02 ~]# crontab -l
    #update system time automatically every 2 minutes
    */2 * * * * /usr/sbin/ntpdate pool.ntp.org
    [root@web02 ~]# date
    Thu Jan 17 02:35:05 CST 2019
    [root@web02 ~]# date
    Fri Jan 18 10:52:48 CST 2019
    You have new mail in /var/spool/mail/root

#### 例子2:每分钟把自己的名字追加到/tmp/name.txt中
    [root@web02 ~]# crontab -l
    #print name to file
    * * * * * echo 'oldboy' >> /tmp/name.txt
    You have new mail in /var/spool/mail/root
    [root@web02 ~]# tail -f /tmp/name.txt
    oldboy
    oldboy
    oldboy
    oldboy
    oldboy
####例子3:每分钟显示当前系统时间追加到/tmp/time.log中
    #print time to file
    * * * * * date +\%F >> /tmp/time.log #%前面加了转义符,是在定时任务里面不认识%,或者使用脚本
    #脚本
    * * * * * /bin/sh /server/scrpits/time.sh >> /tmp/time.log
    #检查
    [root@web02 ~]# tail -f /var/log/cron
    Jan 18 14:46:01 web02 CROND[5537]: (root) CMD (/bin/sh /server/scrpits/time.sh >> /tmp/time.log)
    [root@web02 ~]# tail -f /tmp/time.log 2019-01-18

#### 例子4:定时任务规则中如没有定向到空或者文件中的会很容易导致硬盘inode空间被占满,从而系统服务不正常(一般定时规则后面需要加/dev/null 2>&1)
    #没有定向到空,邮件软件服务开启--定时任务会一直给root用户发送邮件
    You have new mail in /var/spool/mail/root
    ##邮件服务没有开启----大量的小文件会堆积在/var/spool/postfix/maildrop/ 倒致inode满了
    [root@web02 ~]# /etc/init.d/postfix stop
    Shutting down postfix:                                     [  OK  ]
    You have new mail in /var/spool/mail/root
    [root@web02 ~]# ll /var/spool/postfix/maildrop/
    total 4
    -rwxr--r--. 1 root postdrop 502 Jan 18 14:55 6C4CE3F8E9
    #加完>/dev/null 2>&1 后,屏幕上不会提示You have new mail in /var/spool/mail/root

#### 例子5:如何删除大量的小文件
    #环境准备,创建50万小文件
    #直接创建多文件也是会报错的
    [root@web02 maildrop]# touch {1..500000}.txt 
    -bash: /bin/touch: Argument list too long
    #换一种创建方式
    [root@web02 maildrop]# echo {1..500000}.txt |xargs touch
    #无法创建文件,查看磁盘空间与inode号,发现inodek号满了
    [root@web02 maildrop]# touch 123
    touch: cannot touch `123': No space left on device
    [root@web02 maildrop]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda2       7.6G  2.2G  5.1G  30% /
    tmpfs           931M     0  931M   0% /dev/shm
    /dev/sda1       190M   40M  141M  22% /boot
    [root@web02 maildrop]# df -i
    Filesystem     Inodes  IUsed  IFree IUse% Mounted on
    /dev/sda2      512064 512064      0  100% /
    tmpfs          238282      1 238281    1% /dev/shm
    /dev/sda1       51200     39  51161    1% /boot
    #查找文件中的小文件,查看两个目录的小文件,发现maildrop这个目录下有40多万的小文件
    [root@web02 maildrop]# find / -type d -size +1M |xargs ls -hld
    dr-xr-x---. 9 root    root     1.1M Jan 18 15:06 /root
    drwx-wx---. 2 postfix postdrop  13M Jan 18 15:09 /var/spool/postfix/maildrop
    [root@web02 maildrop]# ls /var/spool/postfix/maildrop/ |wc -l
    440166
    [root@web02 maildrop]# ls /root |wc -l
    16
    #删除小文件,发现现在直接删除所有的小文件会报错,这是因为rm不能直接删除大量的小文件,需要换一种方式
    [root@web02 maildrop]# rm -f *
    -bash: /bin/rm: Argument list too long
    [root@web02 maildrop]# ls |xargs rm -f #使用这一种方式
    [root@web02 maildrop]# ls -l
    total 0
    
    #另外一种方式删除小文件所在的目录,但是要注意要先把目录的所有者和权限需要先记住,后面新建的时候进行还原
    
    
    

推荐阅读