定时任务相关介绍

[root@qls ~]# ll  /etc/cron*  -d
drwxr-xr-x. 2 root root  21 Jul 29 08:56 /etc/cron.d          #定时任务的统一存放目录
drwxr-xr-x. 2 root root  57 Jul 29 08:56 /etc/cron.daily     #系统每天执行的定时任务
-rw-------  1 root root   0 Apr 11  2018 /etc/cron.deny        #定时任务的黑名单
drwxr-xr-x. 2 root root  22 Jul 29 08:56 /etc/cron.hourly    #系统每小时执行的定时任务
drwxr-xr-x. 2 root root   6 Jun 10  2014 /etc/cron.monthly #系统每月执行的定时任务
-rw-r--r--  1 root root 451 Jun 10  2014 /etc/crontab          #定时任务主配置文件
drwxr-xr-x. 2 root root   6 Jun 10  2014 /etc/cron.weekly  #系统每周执行的定时任务
 
[root@qls ~]# cat /etc/crontab
SHELL=/bin/bash   #定时任务所使用的命令解释器
PATH=/sbin:/bin:/usr/sbin:/usr/bin  #定时任务所能用到的命令路径
MAILTO=root    #接收邮件
# For details see man 4 crontabs
# 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
# |  |  |  |  |   #  周
# *  *  *  *  * user-name  command to be executed
分时日月周      用户    命令

#跟定时任务相关的文件
[root@qls ~]# ll /var/spool/cron/root  #存放定时任务的配置文件 
total 0
[root@qls ~]# ll /var/log/cron  #定时任务执行的过程   日志

[root@qls ~]# ll /var/spool/mail/ #用户的邮件 

怎样书写定时任务

crontab   #书写定时任务的命令 
选项:
 -e   #编辑定时任务    ===   vi  /var/spool/cron/root
 
 -l   #查看定时任务    ===   cat  /var/spool/cron/root
1. 语法检查
2. 方便简单

#定时任务的规则
* # 每(分时日月周)都执行 
*/5 # 每 5 (分时日月周)执行  每隔多长时间 
/5 
1-3  #时间范围  1-3   连续的时间  1点到3点
1,3  #不连续的时间  1点和3点

00 02 * * *    #每天的凌晨2点整 
00 02 1 * *  #每个月的1号凌晨2点整 
00 02 14 2 *  #每年的2月14日凌晨2点整  
00 02 * * 7  #每周日的凌晨2点整 
00 02 * 6 5  #每年的6月份的每周五的凌晨2点整   
00 02 14 * 7  #每个月的14号或者周日的凌晨2点整
00 02 14 2 7   #每年的2月份的14号或者周日的凌晨2点整
*/10  02 * * *  #每天的凌晨2点每隔10分钟 
* * * * *    #每分钟
00 00 14 2 *   #每年的2月份14号的凌晨0点整
*/5 * * * *    #每隔5分钟
00 02 * 1,5,8 *  #每年的1和5和8月的每天的凌晨2点整
00 02 1-8 * *   #每个月的1到8号的凌晨2点整
00 21 * * *    #每天晚上21点整
45 4 1,10,22 * *  #每个月的1,10,22号 的凌晨4点45分
45 4 1-10 * *    #每个月的1到10号的凌晨4点45分
3,15 8-11 */2 * *  #每个月每隔两天的8到11点的3分和15分的时候
0 23-7/2 * * *    #每天的23点到7点的每隔2个小时的整点
15 21 * * 1-5  #每周一到周五的晚上21点15分
原文地址:https://www.cnblogs.com/chenlifan/p/13409622.html