计划任务1.哪些是计划任务?
根据计划准时准点去执行任务
2.为何须要计划任务?
可以手动执行,不须要人的干预,解放了人力,增强了效率
3.Linux里的计划任务
crond进程:Linux里执行计划任务的程序linux软件,始终保持运行,每隔一分钟就去检测一下系统中的计划任务
使用corntab命令调用crond进程,设置按固定的周期重复执行预先计划好的任务
关于计划任务的一些命令
查看crond进程是否存在:
[root@sanchuang ~]# ps aux|grep crond
root 985 0.0 0.1 36300 3464 ? Ss 3月11 0:01 /usr/sbin/crond -n
root 22074 0.0 0.0 12320 984 pts/0 S+ 11:01 0:00 grep --color=auto crond
重启计划任务:
[root@sanchuang ~]# service crond restart
Redirecting to /bin/systemctl restart crond.service
停止计划任务服务:
[root@sanchuang ~]# service crond stop
Redirecting to /bin/systemctl stop crond.service
启动计划任务服务:
[root@sanchuang ~]# service crond start
Redirecting to /bin/systemctl start crond.service
centos8中也可以使用下边的命令启动:疗效相同
[root@sanchuang ~]# systemctl stop crond
systemctl stop crond 和 service crond stop
4.crontab
crontab是创建计划任务的命令
[root@sanchuang ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
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
* 表示任意的时间
crontab的相关命令:
crontab -e edit 编辑计划任务
crontab -l 查看计划任务 list
使用计划任务的过程:(这儿以执行backup_log.sh脚本为例)
[root@sanchuang lianxi]# crontab -e
第1步:按i进入计划任务的编辑模式,输入下面的内容
0 23 * * * bash /lianxi/backup_log.sh
第2步:按ESC,再输入:wq 退出并且保存
[root@sanchuang lianxi]# crontab -l 查看计划任务
0 23 * * * bash /lianxi/backup_log.sh
怎么晓得计划任务是否顺利执行?
查看crontab的日志:记录什么计划任务的创建、执行、修改、查看等操作
[root@sanchuang log]# tail -f /var/log/cron
Mar 16 15:06:35 sanchuang crontab[22483]: (root) LIST (root) 查看
Mar 16 15:06:49 sanchuang crontab[22484]: (root) BEGIN EDIT (root) 编辑
Mar 16 15:07:07 sanchuang crontab[22484]: (root) REPLACE (root) 修改
Mar 16 15:07:07 sanchuang crontab[22484]: (root) END EDIT (root) 结束编辑
Mar 16 15:07:25 sanchuang crontab[22486]: (root) LIST (root)
Mar 16 15:08:01 sanchuang crond[22151]: (root) RELOAD (/var/spool/cron/root) 加载计划任务开始执行
Mar 16 15:08:01 sanchuang CROND[22491]: (root) CMD (bash /lianxi/sanchuang/create_dir.sh)
Mar 16 15:08:01 sanchuang CROND[22489]: (root) CMDOUT (tar: 从成员名中删除开头的“/”) 脚本的执行输出效果
其中四个数组分别表示:时间、地点、人物和发生的事情
CROND[22491]CROND是进程[22491]是crond的进程号pid号
CMD表示执行命令command
5.关于计划任务时间设定的几个反例1.每周1,5,7的23点40分
4023**1,5,7
2.每位月的1号20:30分
30201**
3.每隔5分钟
*/5****
4.每隔1个小时
0*/1***
5.礼拜1到礼拜5的18:00
018**1-5
6.每隔3天,每晚的13:58执行
5813*/3**
7.每位月的1,10,15,20,25号的2:30
3021,10,15,20,25**
6.几个关于计划任务的小问题1.每位用户都有自己的计划任务吗?
每位用户都有一个自己的储存计划任务的文件件
2.这个用户没有登陆是否计划任务也会执行?
是的
crond这个进程会每隔1分钟就到/var/spool/cron文件夹下加载所有的文件,执行上面的计划任务
3.root用户是否可以去管理其他用户的计划任务?
是的
默认情况下学linux有前途吗,只有root用户可以查看任何人的计划任务linux计划任务整点执行,其他用户只能看自己的
4.计划任务里的命令执行不了?
一般是环境变量的问题
建议使用命令的绝对路径
[root@sanchuang chenlin]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
或则更改环境变量,将crontab添加到环境变量中
7.关于计划任务的一些补充
计划任务的顺风车:linux系统本身也有一些任务须要定时完成的
比如:日志文件的轮转–》每隔一段时间形成一个新的日志文件
linux系统的后台工作举办须要的计划任务储存在那里?
[root@sanchuang lianxi]# cd /etc/cron
cron.d/ cron.daily/ cron.deny cron.hourly/ cron.monthly/ crontab cron.weekly/
cron.d/ 存放计划任务的目录
cron.daily/ 存放每天需要执行的任务
cron.hourly/ 存放每小时需要执行的任务
cron.monthly/ 存放每月需要执行的任务
cron.weekly/ 存放每周需要执行的任务
cron.deny 存放拒绝谁执行计划任务
anacron服务
anacron服务是cron服务的补充程序,起到查漏补缺的作用
填补cron在系统死机后不能执行计划任务的问题,等系统开机后将没有执行的计划任务执行一遍
按天、周或月为单位去检测系统未进行的cron任务
/var/spool/anacron
服务名称:/etc/init.d/anacrond
开机时手动运行,之后将未执行的计划任务执行一遍后linux计划任务整点执行,anacron都会手动停止