day21-定时任务

1. 一次性计划任务at

at命令
用于在指定时间执行命令。at允许使用一套相当复杂的指定时间的方法。它能够接受在当天的hh:mm(小时:分钟)式的时间指定。假如该时间已过去,那么就放在第二天执行。当然也能够使用midnight(深夜),noon(中午),teatime(饮茶时间,一般是下午4点)等比较模糊的词语来指定时间。用户还能够采用12小时计时制,即在时间后面加上AM(上午)或PM(下午)来说明是上午还是下午。 也能够指定命令执行的具体日期,指定格式为month day(月 日)或mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年)。指定的日期必须跟在指定时间的后面。

上面介绍的都是绝对计时法,其实还能够使用相对计时法,这对于安排不久就要执行的命令是很有好处的。指定格式为:now + count time-units,now就是当前时间,time-units是时间单位,这里能够是minutes(分钟)、hours(小时)、days(天)、weeks(星期)。count是时间的数量,究竟是几天,还是几小时,等等。 更有一种计时方法就是直接使用today(今天)、tomorrow(明天)来指定完成命令的时间。

# yum仓库文件的备份
[root@ky201 ~]# cat backup-yum-repo.sh 
#!/bin/bash
mkdir /opt/yum-repo-backup-dir -p
cp -r /etc/yum.repo.d /opt/yum-repo-backup-dir/yum.repo.d-`date "+%Y-%m-%d-%H:%M:%S"`.bak
[root@ky201 ~]# at 5:00PM
warning: commands will be executed using /bin/sh
at> /root/backup-yum-repo.sh
at> <EOT>
job 3 at Fri May 12 17:00:00 2023
按ctrl + d提交任务

1.1. 选项

-f:指定包含具体指令的任务文件
-q:指定新任务的队列名称
-l:显示待执行任务的列表
-d:删除指定的待执行任务
-m:任务执行完成后向用户发送E-mail

1.2. 三天后的下午 5 点 执行/bin/ls

[root@ky201 ~]# date -s "2023-5-11 00:00:00"
Thu May 11 00:00:00 CST 2023
[root@ky201 ~]# at 5pm+3 days
warning: commands will be executed using /bin/sh
at> /bin/ls
at> <EOT>
job 5 at Sun May 14 17:00:00 2023
[root@ky201 ~]# 

1.3. 明天17点钟,输出时间到指定文件内

[root@ky201 ~]# at 17:20 tomorrow
warning: commands will be executed using /bin/sh
at> /bin/ls
at> <EOT>
job 7 at Fri May 12 17:20:00 2023
[root@ky201 ~]# 

1.4. 计划任务设定后,在没有执行之前我们可以用atq命令来查看系统没有执行工作任务

[root@ky201 ~]# atq
3       Fri May 12 17:00:00 2023 a root
4       Sun May 14 17:00:00 2023 a root
5       Sun May 14 17:00:00 2023 a root
7       Fri May 12 17:20:00 2023 a root

1.5. 删除已经设置的任务

[root@ky201 ~]# atrm 3
[root@ky201 ~]# atrm 4
[root@ky201 ~]# atrm 5
[root@ky201 ~]# atq
7       Fri May 12 17:20:00 2023 a root

1.6. 显示已经设置的任务内容

[root@ky201 ~]# at -c 7
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22.....
/bin/ls

1.7. 一次性计划任务使用控制

at.allow 白名单(建议)
at.deny 黑名单
/etc/at.allow优先级大于/etc/at.deny

2. 周期性计划任务crontab

2.1. 写计划任务

-e:编辑该用户的计时器设置
-l:列出该用户的计时器设置
-r:删除该用户的计时器设置
-u<用户名称>:指定要设定计时器的用户名称

crontab -e 编辑用户定时任务配置文件/var/spool/cron/root

[root@ky201 ~]# crontab -e
# 每1分钟执行一次command
* * * * * command

# 每小时的第3和第15分钟执行
3,15 * * * * command

# 在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * command

# 每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * command

# 每个星期一的上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 command

# 每晚的21:30重启smb 
30 21 * * * /etc/init.d/smb restart

# 每月1、10、22日的4 : 45重启smb 
45 4 1,10,22 * * /etc/init.d/smb restart

# 每周六、周日的1:10重启smb
10 1 * * 6,0 /etc/init.d/smb restart

# 每天18 : 00至23 : 00之间每隔30分钟重启smb 
0,30 18-23 * * * /etc/init.d/smb restart

# 每星期六的晚上11:00 pm重启smb 
0 23 * * 6 /etc/init.d/smb restart

# 每一小时重启smb 
0 */1 * * * /etc/init.d/smb restart

# 晚上11点到早上7点之间,每隔一小时重启smb
0 23-7/1 * * * /etc/init.d/smb restart

# 每月的4号与每周一到周三的11点重启smb 
0 11 4 * mon-wed /etc/init.d/smb restart

# 一月一号的4点重启smb
0 4 1 jan * /etc/init.d/smb restart

# 每小时执行/etc/cron.hourly目录内的脚本
01 * * * * root run-parts /etc/cron.hourly


# 也可以编辑配置文件来设置计划任务(所有用户公用)
[root@ky201 ~]# cat /etc/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

# /etc/cron.d/目录下存放的文件格式和/etc/crontab的格式是一样的,是一个crontab配置文件的扩展目录,默认情况下也会读取这个目录里面定义的周期计划任务
[root@ky201 ~]# cat /etc/cron.d/0hourly 
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly

[root@ky201 ~]# ls -l /etc/cron.deny 
-rw-r--r--. 1 root root 0 May 23  2022 /etc/cron.deny
/etc/cron.allow白名单要强于/etc/cron.deny黑名单

查看cron计划任务操作日志
[root@ky201 ~]# cat /var/log/cron

3. 定时同步时间

crontab -e
*/2 * * * * /sbin/ntpdate ntp.aliyun.com >/dev/null 2>&1

4. 定时备份

# yum仓库文件的备份
[root@ky201 ~]# cat backup-yum-repo.sh 
#!/bin/bash
cp -a /etc/yum.repo.d /opt/yum-repo-backup-dir/yum.repo.d-`date "+%Y-%m-%d-%H:%M:%S"`.bak


#备份文件超过10个,删除旧的5个备份文件
if [ `ls /opt/yum-repo-backup-dir | wc -l` -ge 10 ]
then
for i in `ls -tr /opt/yum-repo-backup-dir | head -10`
do
rm -rf /opt/yum-repo-backup-dir/$i
done
fi

echo "当前文件数量:`ls /opt/yum-repo-backup-dir | wc -l`"

5. 定时巡检脚本

  • 巡检指标
主机基本信息
1.主机名
2.ip地址
3.系统版本
4.内核版本

进程信息:
1.进程总数
2.僵尸进程数量

5.1. 麒麟系统巡检脚本

#!/bin/bash
#desc: 巡检脚本
#author zbl

#基本信息
#1.变量
hostname=`hostname`
ip=`ip a s ens33 | awk -F '[ /]+' 'NR==3 {print $3}'`
os=`hostnamectl | awk -F ':' '/Operating System/ {print $2}'`
kernel=`uname -r`

#2.使用
cat<<EOF
###########################################
主机基本信息
1.主机名: $hostname
2.ip地址: $ip
3.系统版本: $os
4.内核信息: $kernel
###########################################
EOF

#进程信息
#1.变量
procnum=`top -bn1 | awk 'NR==2{print $2}'`
zombie=`top -bn1 | awk 'NR==2{print $(NF-1)}'`

#2.使用
cat<<EOF
进程信息
1.进程总数: $procnum
2.僵尸进程总数: $zombie
EOF

5.2. ubuntu系统巡检脚本

#!/bin/bash
#desc: check system info
#author: zbl

#基本信息
#1.vars变量
hostname=`hostname`
ip=`ip a s ens33 | awk -F '[ /]+' 'NR==4{print $3}'`
os=`hostnamectl | awk -F ':' '/Operating System/{print $2}'`
kernel=`uname -r`

#2.使用
cat<<EOF
##################################
基本信息
1.主机名: $hostname
2.ip地址: $ip
3.系统版本: $os
4.内核版本: $kernel
##################################
EOF

#进程信息
#1.vars变量
proc_total=`top -bn1 | awk 'NR==2{print $2}'`
zombie=`top -bn1 | awk 'NR==2{print $(NF-1)}'`

#2.使用
cat<<EOF
进程信息
1.进程总数: $proc_total
2.僵尸进程数量: $zombie
EOF

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱包林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值