第八周作业

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

解答:a.命令行模式下输入 top
      b.输入M

执行效果:

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

解答:编写脚本pingtest如下:
for循环实现:
#! /bin/bash
ip_pre=192.168.0
  for i in {1..254};do
    ping -c1 -w1 $ip_pre.$i &>/dev/null
    if [ $? -eq 0 ];then
      echo "$ip_pre.$i success!"
    else
      echo "$ip_pre.$i fail!"
  fi
done

white循环实现
#!/bin/bash
ip_pre=192.168.0
declare -i i=1
  while [ $i -lt 255 ];do
    ping -c1 -W1 $ip_pre.$i &> /dev/null
    if [ $? -eq 0 ];then
      echo "$ip_pre.$i success!"
    else
      echo "$ip_pre.$i fail!"
    fi
    let i++
done

执行效果:

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

解答:
打包命令:
tar -zcf etcbak-date -d "-1 day" +%Y-%m-%d-%H.tar.xz /etc &> /dev/null
设置定时任务:crontab -e
30 1 * * 1-5 /usr/bin/tar -zcf etcbak-date -d "-1 day" +%Y-%m-%d-%H.tar.xz /etc &> /dev/null

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

解答: 磁盘检查脚本如下:
#! /bin/bash
useage=df |sed -rn '/^/dev/sd*/s#^([^[:space:]]+).* ([[:digit:]]+)%.*#2#p'
devname=df |sed -rn '/^/dev/sd*/s#^([^[:space:]]+).* ([[:digit:]]+)%.*#1#p'
if [ $useage -gt 1 ];then
  echo "$devname will be full,$useage%"|mail -s "alert" root@localhost
fi

设置定时任务:crontab -e
*/10 * * * * /bin/sh /root/useage_check.sh &> /dev/null

执行效果:

原文地址:https://www.cnblogs.com/mwd-123/p/12129384.html