一、写一个脚本,完成如下功能 传递一个参数给脚本,此参数为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@localhost scripts]# mkdir /scripts/backups
[root@localhost scripts]# vim compress_work.sh
[root@localhost scripts]# bash compress_work.sh
[root@localhost scripts]# ll /backups/
二、报警脚本
要求如下:
根分区剩余空间小于20%
发送告警邮件给自己
配合crond每5分钟检查一次脚本
#根剩余内存
[root@localhost scripts]# df | grep /dev/mapper/openeuler-root | tr -s " " | cut -d " " -f 4
24483368
#根总共内存
[root@localhost scripts]# df | grep /dev/mapper/openeuler-root | tr -s " " " " | cut -d " " -f 2
27687012
#占比
[root@localhost scripts]# num=` echo $mem*100 / $total | bc`
[root@localhost scripts]# echo $num
88
#shell脚本编辑
[root@localhost scripts]# vim work.sh
#!/bin/bash
#根剩余内存
a=`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 "警告" 3126929762@qq.com
fi
#定时
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
5 * * * * bash /scripts/work.sh
[root@localhost ~]# crontab -l
5 * * * * bash /scripts/work.sh