概念
定时(计划)任务 未来的某时间点执行一次某任务或者周期性的运行某个任务,执行结果会通过邮件的形式发送给用户
定时任务是非登录式shell
分类
- 定点执行一次某任务 at,batch
- 周期性的运行任务 crontab
用户及系统定时任务
系统任务调度
概念
系统周期性执行的工作 比如写缓存数据到硬盘、日志清理等
crond系统服务
Linux 系统上面原本就有非常多的计划性工作,由默认启动的系统服务 crond
控制,crond是Linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,即crond进程定期(每分钟)检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。
【查看crond服务状态】
service crond status(CentOS6)
systemctl status crond(CentOS7)
配置文件
系统任务调度配置文件 /etc/crontab
/etc/cron.d/
[root@zycentos7 ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
内容 | 含义 |
---|---|
SHELL | 系统要使用的shell |
PATH | 系统执行命令的路径 |
MAILTO | crond的任务执行信息接收人(变量的值为空,表示不发送) |
HOME | 在执行命令或者脚本时使用的主目录 |
注意事项
- 每一行定义一个周期性任务,共7个字段
* * * * * user-name command to be executed
内容 | 含义 |
---|---|
* * * * * | 定义周期性时间 |
user-name | 运行任务的用户身份 |
command to be executed | 任务 |
- 命令使用绝对路径或者自定义PATH环境变量 此处的环境变量不同于用户登录后获得的环境
- 执行结果邮件发送给MAILTO指定的用户
使用者权限文件
文件 | 含义 |
---|---|
/etc/cron.deny | 该文件中所列用户不允许使用crontab命令 |
/etc/cron.allow | 该文件中所列用户允许使用crontab命令 |
/var/spool/cron/ | 所有用户crontab文件存放的目录,以用户名命名 |
时间表示法
- 特定值
给定时间点有效取值范围内的值 day of week和day of month一般不同时使用 - *
给定时间点有效取值范围内的值 每…… - 离散取值,
在时间点上使用逗号分隔的多个值 #,#,# - 连续取值-
在时间点上使用-连接开头和结束 #-# - 在指定时间点上,定义步长
/#
注意:
(1) 指定的时间点不能被步长整除时,其意义将不复存在
(2) 最小时间单位为“分钟”,想完成“秒”级任务,得需要额外借助于其它机制:定义成每分钟任务,而在利用脚本实现在每分钟之内,循环执行多次
示例
- 每小时的第3分钟执行一次
3 * * * *
- 每周五的4点3分执行一次
3 4 * * 5
- 每月的7号的6点5分执行一次
5 6 7 * *
- 每年的10月9号8点7分执行一次
7 8 9 10 *
- 每周三和周日的8点9分各执行一次
9 8 * * 3,7
- 每周三和周日的8点和20点各执行一次
0 8,20 * * 3,7
- 每周一到周五的早9点到晚18点的整点各执行一次
0 9-18 * * 1-5
- 每5分钟执行一次
*/5 * * * *
用户任务调度
用户定期要执行的工作 比如用户数据备份、定时邮件提醒等
- 用户设置的计划任务由
crontab
命令控制 - 用户定义的crontab文件都被保存在
/var/spool/cron
目录中,以用户名为文件名
[root@zycentos7 ~]# cat /var/spool/cron/USERNAME
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=USERNAME
# 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
# | | | | |
# * * * * * command to be executed
注意:
(1) 每行定义一个cron任务,共6个字段
(2) 命令使用绝对路径或者自定义PATH环境变量 此处的环境变量不同于用户登录后获得的环境
(3) 邮件发送给当前用户
crontab 计划任务
crontab -e
编辑计划任务
crontab -l
查看计划任务
crontab -r
移除所有任务 即删除 /var/spool/cron/USERNAME
文件
crontab -ri
移除时交互
crontab -u user
root用户可为指定用户管理cron任务
注意
- 注释
- 用绝对路径
- 信息重定向
/dev/null 2>&1
- COMMAND中的%需要转义,但放置于单引号中的%不用转义
练习
[root@zycentos7 ~]# crontab -e
SHELL=/bin/bash #指定shell类型
PATH=/sbin:/bin:/usr/sbin:/usr/bin #指定环境变量
MAILTO=root #指定运行用户
#test crontab #注释
*/1 * * * * /bin/echo "this is a test from root_cron.." >> /root/cron_test
#备份etc目录
1,2 * * * * /bin/tar -zvcf /data/etc.`date "+\%Y\%m\%d\%H\%M"`.tar.gz /etc/ >/dev/null 2>&1
#test1
1,2 18-20 * * * /bin/echo "`date "+\%Y-\%m-\%d \%H:\%M:\%S"` test1.." >> /root/test_cron
#test2
1,2 18-20 */2 * * /bin/echo "`date "+\%Y-\%m-\%d \%H:\%M:\%S"` test2.." >> /root/test_cron
#test3
1,2 18-20 * * 1 /bin/echo "`date "+\%Y-\%m-\%d \%H:\%M:\%S"` test3.." >> /root/test_cron
[root@zycentos7 ~]# tail -f /var/log/cron ;实时查看日志信息
- 每1分钟执行一次command
- 每小时的第3和第15分钟执行
- 在上午8点到11点的第3和第15分钟执行
- 每隔两天的上午8点到11点的第3和第15分钟执行
- 每个星期一的上午8点到11点的第3和第15分钟执行
- 每晚的21:30重启smb
- 每月1、10、22日的4 : 45重启smb
- 每周六、周日的1 : 10重启smb
- 每天18 : 00至23 : 00之间每隔30分钟重启smb
- 每星期六的晚上11 : 00 pm重启smb
- 每一小时重启smb
- 晚上11点到早上7点之间,每隔一小时重启smb
- 每月的4号与每周一到周三的11点重启smb
- 一月一号的4点重启smb
#test1-5
* * * * * command
3,15 * * * * command
3,15 8-11 * * * command
3,15 8-11 */2 * * command
3,15 8-11 * * 1 command
#test6-14
30 21 * * * /etc/init.d/smb restart
45 4 1,10,22 * * /etc/init.d/smb restart
10 1 * * 6,7 /etc/init.d/smb restart
*/30 18-23 * * * /etc/init.d/smb restart
0 23 * * 6 /etc/init.d/smb restart
* */1 * * * /etc/init.d/smb restart
* 23-7/1 * * * /etc/init.d/smb restart
0 11 4 * * 1-3 /etc/init.d/smb restart
0 4 1 1 * /etc/init.d/smb restart
思考题
某任务在指定的时间因关机未能执行,下次开机会不会自动执行?不会
如果期望某时间因故未能按时执行,下次开机后无论是否到了相应时间点都要执行一次,可使用 anacron
实现
设置crontab服务开机自启动
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# chkconfig crond on
Note: Forwarding request to 'systemctl enable crond.service'.
[root@zycentos7 ~]# systemctl status crond ;查看crontab服务状态
at 定点任务
内容 | 含义 |
---|---|
at | 在特定的时间执行一次性的任务 |
atq 或 at -l | 列出用户的计划任务 |
atrm 或 at -d | 根据Jobnumber删除at任务 |
batch | 在系统负荷允许的情况下执行at任务,换言之,就是在系统空闲的情况下才执行at任务 |
[root@zycentos7 ~]# yum -y install at
[root@zycentos7 ~]# at
Garbled time
[root@zycentos7 ~]# systemctl start atd ;启动服务
示例1 5分钟后执行
[root@zycentos7 ~]# at now +5 minutes
at> echo "5 minnutes" > a.txt
at> <EOT> ;Ctrl+d
job 5 at Mon Nov 11 21:05:00 2019
示例2 8分钟后执行
[root@zycentos7 ~]# at now +8 minutes
at> echo "8 minutes" > b.txt
at> <EOT>
job 6 at Mon Nov 11 21:09:00 2019
示例3 查看待处理作业
[root@zycentos7 ~]# atq
1 Mon Nov 11 21:05:00 2019 a root
2 Mon Nov 11 21:09:00 2019 a root
root用户将列出所有用户的任务,结果的输出格式为:作业号、日期、小时、队列和用户名
示例4 根据作业ID查看作业内容
[root@zycentos7 ~]# at -c 5
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
HOSTNAME=zycentos7; export HOSTNAME
……
echo "5 minnutes" > a.txt
marcinDELIMITER469a164c
示例5 删除待处理作业
[root@zycentos7 ~]# atrm 1
[root@zycentos7 ~]# atq
2 Mon Nov 11 21:09:00 2019 a root
linux系统bash加载环境变量的过程
登录式shell
/etc/profile
--> ~/.bash_profile
--> ~/.bashrc
--> /etc/bashrc
非登录式shell
~/.bashrc
--> /etc/.bashrc
--> /etc/profile.d/*.sh
定时任务是非登录式Shell,其读取的配置文件为 ~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh,加载环境变量的顺序略过了/etc/profile,因此导致环境变量不完整,可能导致脚本执行失败
解决方法
- 可以在脚本中加上环境变量的路径
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/data/nginx/sbin:/root/bin
- 可以在计划任务中将非登录shell未加载的/etc/profile文件加载
#backup data
0 0 * * * root ./etc/profile;/usr/bin/fileback tar >/dev/null 2>&1