任务计划
在linux中,任务计划分两种:未来时间只执行一次 和 周期性执行
•未来的某时间点执行一次任务
at
batch:系统自行选择空闲时间去执行此处指定的任务
•周期性运行某任务
cron
例:开机自动同步时间方法
1、首先保证能与安装ntp服务的server间能通讯,然后编辑etc/chrony.conf 文件,添加红色方框内你要同步时间的Server 的IP地址 ,在Centos6.9 上则要编辑etc/ntp.conf文件
[root@centos7 ~]#vim /etc/chrony.conf
2、设置chronyd服务为开机自动启动,然后手动开启chronyd服务,Centos6.9 中用 chkconfig ntpd on 设为开机自启动 ,用service ntpd start 开启服务
[root@centos7 ~]#systemctl enable chronyd
[root@centos7 ~]#systemctl start chronyd
3、查看chronyd 服务状态,running为启动中,正常。
[root@centos7 ~]#systemctl staus chronyd
at任务--用法
包:at
at命令:at [option] TIME
常用选项:
例:
[root@centos7 ~]#at 22:14 at> rm /mnt/cdrom/f1 -rf at> <EOT> job 1 at Sun May 6 22:14:00 2018
查看计划任务列表 ll /var/spool/at
-V 显示版本信息:
-l: 列出指定队列中等待运行的作业;相当于atq
-d: 删除指定的作业;相当于atrm
-c: 查看具体作业任务
cat -c 2 查看第二个计划任务
-f /path/from/somefile:从指定的文件中读取任务
at -f a.txt now +1minutes 读取a.txt文件内的计划任务 在1分钟后执行
-m:当任务被完成之后,将给用户发送邮件,即使没有标准输出
注意:作业执行命令的结果中的标准输出和错误以邮件通知给相关用户
TIME:定义出什么时候进行 at 这项任务的时间
HH:MM [YYYY-mm-dd]
noon, midnight, teatime(4pm)
tomorrow
now+#{minutes,hours,days, OR weeks}
at时间格式
HH:MM 02:00
在今日的 HH:MM 进行,若该时刻已过,则明天此时执行任务
HH:MM YYYY-MM-DD 02:00 2016-09-20
规定在某年某月的某一天的特殊时刻进行该项任务
HH:MM[am|pm] [Month] [Date]
04pm March 17
17:20 tomorrow
HH:MM[am|pm] + number [minutes|hours|days|weeks]
在某个时间点再加几个时间后才进行该项任务
now + 5 minutes at now +15minutes 过15分钟执行任务
如果计划任务内有标准输出,计划任务不会显示在终端,而是右键发送给你
02pm + 3 days
at任务
执行方式:
1)交互式 2)输入重定向 3)at –f 文件
依赖与atd服务,需要启动才能实现at任务
at队列存放在/var/spool/at目录中
/etc/at.{allow,deny}控制用户是否能执行at任务
白名单:/etc/at.allow 默认不存在,只有该文件中的用户才能执行at命令
echo wang > /etc/at.allow 将wang 添加至白名单,如果同时在白名单和黑名单里, 那以白名单为主
黑名单:/etc/at.deny 默认存在,拒绝该文件中用户执行at命令,而没有在at.deny 文件中的使用者则可执行
如果两个文件都不存在,只有 root 可以执行 at 命令
周期性任务计划cron
确保crond守护处于运行状态:
CentOS 7:
systemctl status crond
CentOS 6:
service crond status
计划周期性执行的任务提交给crond,到指定时间会自动运行
系统cron任务:系统维护作业
/etc/crontab
用户cron任务:
crontab命令
日志:/var/log/cron
系统cron任务:/etc/crontab
注释行以 # 开头
详情参见 man 5 crontab
# 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
例如:晚上9点10分运行echo命令
10 21 * * * centos /bin/echo "Howdy!"
例:在今年每月每个周一到周五的2点整将etc下的文件打包到date的目录下
1、首先用vim打开etc下的crontab文件
[root@centos7 ~]#vim /etc/crontab
2、按照格式,设置为每周一至周五的2点以root账户将etc下的文件打包压缩至date目录下 (*/10 为每10分钟执行一次)
3、为了方便,我们将系统时间设置为1点59分,等一分钟后查看执行效果,成功!
[root@centos7 ~]#date 050701592018 Mon May 7 01:59:00 CST 2018 [root@centos7 etc]#ls /date etc.tar.gz f1 F1 F1.blk
普通用户创建计划任务
[root@laobai /date#crontab -e
在每天的17点56分在date目录下创建一个f11的文件夹
测试
然后删除此计划任务
[root@laobai /date#cd /var/spool/cron [root@laobai /var/spool/cron#ll total 4 -rw------- 1 root root 28 May 8 17:59 root [root@laobai /var/spool/cron#crontab -r [root@laobai /var/spool/cron#ll total 0
时间格式
@reboot Run once after reboot
例:
@yearly 0 0 1 1 *
@annually 0 0 1 1 *
@monthly 0 0 1 * *
@weekly 0 0 * * 0
@daily 0 0 * * *
@hourly 0 * * * *
示例:每3小时echo和wall命令
0 */3 * * * centos /bin/echo “howdy”;/usr/bin/wall “welcome to Magedu!”
系统的计划任务:
crontab [-u user] [-l | -r | -e] [-i]
-l: 列出所有任务
-e: 编辑任务
-r: 移除所有任务
-i:同-r一同使用,以交互式模式移除指定任务
-u user: 仅root可运行,指定用户管理cron任务
/etc/crontab
/etc/cron.d/ 配置文件
/etc/cron.hourly/ 脚本
/etc/cron.daily/ 脚本
/etc/cron.weekly/ 脚本
/etc/cron.monthly/ 脚本
[root@rhel6 ~]# cat /etc/crontab #配置文件 SHELL=/bin/bash #执行任务命令的shell类型 PATH=/sbin:/bin:/usr/sbin:/usr/bin #执行任务的环境变量 MAILTO=root #执行任务的用户 HOME=/
@reboot :系统启动后执行
@yearly 0 0 1 1 * :每年的1月1号00:00执行一次任务
@annually 0 0 1 1 * :每年的1月1号00:00执行一次任务
@monthly 0 0 1 * * :每月的1号00:00执行一次任务
@weekly 0 0 * * 0 :每周日的00:00执行一次任务
@daily 0 0 * * * :每天的00:00执行一次任务
@hourly 0 * * * * :每小时执行一次任务
注意:对于cron任务来讲,%有特殊用途;如果在命令中要使用%,则需要转义