Linux中定制计划任务

什么是计划任务

让系统在将来的指定时间点执行某些任务(程序);任务可以周期性执行也可以仅仅执行一次。Linux系统中计划任务:at和cron服务是操作系统内置的2个服务,默认情况是安装好的

at服务:指定某个时间一次性执行某个任务,依赖于系统后台atd进程

cron服务:指定时间周期性执行某个任务,依赖于系统后台crond进程

batch:  在系统负载不高的时候一次性执行一次,系统不忙的时候运行,平均负载小于0.8的时候

一次性调度执行 at

1、at安装:

[root@liyingchao backup]# rpm -q at
at-3.1.13-24.el7.x86_64
[root@liyingchao backup]# yum install at -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.nju.edu.cn
 * extras: mirrors.nju.edu.cn
 * updates: mirrors.nju.edu.cn
软件包 at-3.1.13-24.el7.x86_64 已安装并且是最新版本
无须任何处理

2、启动at服务

[root@liyingchao backup]# service atd start
Redirecting to /bin/systemctl start  atd.service
[root@liyingchao backup]# ps aux|grep atd
root      86399  0.0  0.0  25904   956 ?        Ss   11:48   0:00 /usr/sbin/atd -f
root      92990  0.0  0.0 112824   980 pts/2    S+   19:18   0:00 grep --color=auto atd

3、相关例子

[root@lamp-test ~]# at 11:00 创建一个一次性执行的计划任务
at> bash /root/wang.sh
at> <EOT>
job 5 at Wed Dec 22 11:00:00 2021
[root@lamp-test ~]# at -l
3	Wed Dec 22 10:20:00 2021 a root
5	Wed Dec 22 11:00:00 2021 a root
[root@lamp-test ~]# 

[root@lamp-test ~]# atrm  3  删除计划任务编号为3的任务
[root@lamp-test ~]# at -l  查看计划任务列表
5	Wed Dec 22 11:00:00 2021 a root
[root@lamp-test ~]# 

周期性计划任务 crontab

当需要周期性的重复执行任务是可以使用cron服务;该服务每分钟检查一次,并执行符合条件的任务

1、存放crontab创建的周期性计划任务

[root@liyingchao spool]# cd /var/spool/cron
[root@liyingchao cron]# ls
root
[root@liyingchao cron]# cat root
30 3 * * * bash /root/sc.sh
30 18 * * * bash /backup/backup_log.sh

用户: root  

root用户设置了一个计划任务,每天的9:30 执行backup.sh脚本

用户设置了计划任务,但是这个用户没有登录系统,是否每天还是会执行?
答案: 会执行,没有登录也会执行

2、cron服务配置文件和cron服务的日志文件
cron服务配置文件
位于文件:/etc/crontab 

[root@liyingchao etc]# pwd
/etc
[root@liyingchao etc]# cat 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


cron服务的日志文件:记录创建、查看、执行等相关的操作,知道一个计划是否执行
位于文件:/var/log/cron 

[root@liyingchao log]# tail -f cron
Dec 22 18:40:01 liyingchao CROND[92496]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Dec 22 18:50:01 liyingchao CROND[92596]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Dec 22 19:00:01 liyingchao CROND[92759]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Dec 22 19:01:01 liyingchao CROND[92784]: (root) CMD (run-parts /etc/cron.hourly)
Dec 22 19:01:01 liyingchao run-parts(/etc/cron.hourly)[92784]: starting 0anacron
Dec 22 19:01:01 liyingchao run-parts(/etc/cron.hourly)[92796]: finished 0anacron
Dec 22 19:10:01 liyingchao CROND[92884]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Dec 22 19:20:01 liyingchao CROND[93011]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Dec 22 19:30:01 liyingchao CROND[93113]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Dec 22 19:40:01 liyingchao CROND[93220]: (root) CMD (/usr/lib64/sa/sa1 1 1)

anacron服务

弥补cron在系统关机后不能执行计划任务的问题
不能替代cron
按天、周或月为单位去检查系统未进行的cron任务
/var/spool/anacron
服务名称:/etc/init.d/anacrond
开机时自动运行,然后将未执行的计划任务执行一遍后,anacron 就会自动停止

练习题

1.编写一个脚本/backup/backup_log.sh实现备份/var/log目录下的所有文件到/backup目录下,要求文件名是包含当天日期,精确到秒,文件名例如:2016-6-6-2_30_20-log.tar.gz。
  同时要求删除/backup目录下七天前的备份文件,只保留最近7天的

  2.以root用户的身份去执行,计划任务的要求是每天的4:30执行上面的这个脚本/backup/backup_log.sh

[root@liyingchao backup]# pwd
/backup
[root@liyingchao backup]# cat backup_log.sh 
#!/bin/bash
mkdir -p /backup
tar -czf  /backup/$(date +%F_%H_%M_%S)-log.tar.gz   /var/log/*
find /backup -mtime +7 -type f -name  "*.tar.gz" -exec rm -rf {} \;
[root@liyingchao backup]# crontab -l
30 3 * * * bash /root/sc.sh
30 18 * * * bash /backup/backup_log.sh
[root@liyingchao backup]# ls
2021-12-22_18_30_02-log.tar.gz  backup_log.sh

猜你喜欢

转载自blog.csdn.net/weixin_60067160/article/details/122092849