RHCE——第五次作业

文章描述了如何编写一个脚本,根据传入的参数执行gzip、bzip2或xz的归档压缩,并在根分区剩余空间低于20%时发送告警邮件。脚本还包含了crontab定时任务的设置。
摘要由CSDN通过智能技术生成

1.写一个脚本,完成如下功能 传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;


(1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz;
(2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2;
(3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.xz;
(4) 其它任意值,则显示错误压缩工具,并执行非正常退出;

[root@student scripts]# vim 21.sh     
#!/bin/bash
echo 1:gzip                                         
echo 2:bzip
echo 3:xz
[ -d /backups ] || mkdir /backups        
read -p "请输入序号指定打包压缩类型:" NAME   
case "$NAME" in
    1)
      tar -Pczvf /backups/etc-20160613.tar.gz /etc/*  &>/dev/null
      ;;
    2)
      tar -Pcjvf /backups/etc-20160613.tar.bz2  /etc/* &>/dev/null
      ;;
    3)
       tar -PcJvf /backups/etc-20160613.tar.xz  /etc/* &>/dev/null

       ;;
     [^0-3])
       echo "错误" 
       ;;
esac

#运行测试脚本

[root@student scripts]# bash 21.sh
1:gzip
2:bzip
3:xz
请输入序号指定打包压缩类型:5
选择错误

[root@student scripts]# ll /backups   
ls: cannot access '/backups': No such file or directory
[root@student scripts]# bash 21.sh 
1:gzip
2:bzip
3:xz
请输入序号指定打包压缩类型:1
[root@student scripts]# ll /backups         
total 7552
-rw-r--r--. 1 root root 7732782 Sep 21 09:43 etc-20160613.tar.gz

2.报警脚本,要求如下:


根分区剩余空间小于20%
发送告警邮件给自己
配合crond每5分钟检查一次脚本

#编辑脚本,因为我根内存使用很少,我将条件改为大于20%时发邮件

[root@localhost ~]# vim /progress/03.sh

#!/bin/bash

#根剩余内存

mem=`df  | grep /dev/mapper/openeuler-root | tr -s " " " " | cut -d " " -f 4`

#根总共内存

total=`df  | grep /dev/mapper/openeuler-root | tr -s " " " " | cut -d " " -f 2`

num=` echo $mem*100 / $total | bc`

if [ $num -lt 20 ];then

        echo 内存报警 | mail -s "报警" -a /a.txt 18200560891@163.com < /a.txt

fi

#编辑例行性任务,每5分钟执行一次

[root@localhost ~]# crontab -e

*/5 * * * * bash /progress/03.sh

#查看crontab正在运行任务

[root@localhost ~]# crontab -l

*/5 * * * * bash /progress/03.sh

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值