第8周作业

1、显示统计占用系统内存最多的进程,并排序。

ps aux --sort -%mem | head

2、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

1. for 循环实现

vim scanip.sh

#!/bin/bash
set -u
set -e
NETID=192.168.0
for HOSTID in {1..254};do
    {
        if ping -c1 -w1 $NETID.$HOSTID &> /dev/null;then
            echo "$NETID.$HOSTID is success!"
        else
            echo "$NETID.$HOSTID is fail!"
        fi
    } &
done
wait

 

2. while 循环实现

vim scanip.sh

#!/bin/bash
set -u
set -e
NETID=192.168.0
declare -I HOSTID=1
while [ $HOSTID -le 254 ];do
    {
        if ping -c1 -w1 $NETID.$HOSTID &> /dev/null ;then
            echo “$NETID.$HOSTID is success!” | tee -a ping.txt
        else
            echo “$NETID.$HOSTID is fail!”
        fi
    } &
let HOSTID++
done
wait

3、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间

vim /root/bin/etcbak.sh

#!/bin/bash
dir='/backup'
[ -d "$dir" ] || mkdir $dir
tar Jcpf "$dir"/`date -d "-1 day" +%F-%H`.tar.xz /etc &> /dev/null

chmod +x /root/bin/etcbak.sh

systemctl enable crond

systemctl start crond

vim /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
30 1 * * 1-5 root /root/bin/etcbak.sh

4、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80%,就发送邮件报警

vim /root/bin/checkdisk.sh

#!/bin/bash
WARNING=80
USE=df | sed -nr "/\/dev\/sd/s@.* ([0-9]+)% .*@\1@p" | sort -nr | head -n1
#awk替代sed方法USE=df | awk -F " +|%" '/\/dev\/sd/{print $5}' | sort -nr | head -n1
[ "$USE" -gt $WARNING ] && wall disk will be full

vim /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
*/10  *  *  *  1-5  root  /root/bin/checkdisk.sh

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值