Linux计划任务:
未来的某个时间执行一次任务,或者周期性执行某个任务,执行结果会通过邮件通知
定时任务:at batch
周期性任务:crontab
[root@localhost ~]# service crond status
crond (pid 1231) is running…
系统任务调度:/etc/crontab
用户任务调度: /var/spool/cron
mail -s “邮件主题” -c “抄送地址” -b"密送地址" -f"发送人地址" -F"发送人姓名" (Ctrl+d:终止)
echo “test” |mail -s test localhost
at
1、时间定义
hh:mm 12:00
noor teatime
AM(上午) PM 8am 9pm
2019-8-11 2019/8/11
+ 5 minutes hours days weeks
today tomorrow
2、语法结构
at 选项 参数
-q 列出任务
-f 从文件中读取
-l 列出任务
-d 指定删除
-v 列出已执行未删除的任务
-V 版本号
-c 查看作业内容
atq
atrm
[root@localhost ~]# service atd status
atd (pid 1773) is running…
(若没启动,输入:service atd start)
[root@localhost ~]# at now +5 minutes
at> echo"hello1234"
at> < EOT>
job 5 at 2019-08-11 19:38
[root@localhost ~]# atq
5 2019-08-11 19:38 a root
[root@localhost ~]# at -c 5
[root@localhost ~]# at -V
at version 3.1.10
cron命令
[root@localhost ~]# 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
分 时 日 月 周
1、特定值
40 8 * * *
2、*
* 8 * * *
3、连续值
8-10 9-17
4、离散值
8,10,12:8点、10点、12点
5、定义时间点 时间步长
/4:每隔4个小时
注意:定以步长需要能被整除
5 * * * *:每小时的第五分钟执行一次,每小时
3 4 * * 5:每周五的四点零三分执行,每周
5 6 7 * *:每月7号六点零五分执行,每月
7 8 9 10 *:每年的十月九号八点零七分执行,每年
8 9 * * 3,4:每周三和每周四的九点零八分执行
0 8,9,20 * * 6,7:每周六周天的八点、九点、20点整执行
*/5 * * * *:每隔5分钟执行一次
crontab语法结构
crontab [-u user] [-l | -r | -e] [-i] [-s]
-e:编辑任务
-l:列出任务列表
-r:移除任务(一次性删除)
-i:移除时确认,与-r一起使用
-u:加用户
1、每一分钟执行一次
* * * * * command
2、每小时的第三分钟和第十五分钟执行一次
3,15 * * * * command
3、从早上的8点到12点的第3分钟和第15分钟执行一次
3,15 8-12 * * * command
4、每隔两天的上午11点到18点之间的第三分钟和第15分钟执行一次
3,15 11-18 */2 * * command
5、每晚18点重启网卡配置
0 18 * * * service network restart
6、每周天凌晨1点重启apache服务
0 1 * * 7 service httpd restart
7、每小时重启一次command服务
0 * * * * command
8、每月的4号与每周的周一到周五执行网卡重启
* * 4 * 1-5 service network restart
9、每年的1月1号重启网卡
* * 1 1 * service network restart
10、每晚的11点到早上7点之间,每小时重启一次
0 23-7 * * * command
计划任务规范:
1、脚本要规范,脚本存放位置要规范,便于维护
2、定时任务要写注释
3、执行的脚本要带绝对路径
4、执行结果的标准输出或者错误输出 >/dev/null
5、可以以命令完成的任务,也要写在脚本
6、分用户执行