第八周-计划任务小演练

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

[root@centos7-1data]#ps aux | sort -k4 -nr | head -n1

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

for语句实现IP网段的piing通 

[root@centos7 ~]# cat ip
cat: ip: No such file or directory
[root@centos7 ~]# cat ip.sh 
#!/bin/bash
#192.168.0.0/24
NUM=`seq 1 254`

for IP in ${NUM};do
   HOST_IP="192.168.0.${IP}"
   ping -c 2 -w 3 ${HOST_IP}
   if [ $? -eq 0 ];then
      echo "success!" && echo ${HOST_IP} >> /tmp/ip.txt
   else 
      echo "fail!"  && echo ${HOST_IP} >> /tmp/ip1.txt
   fi 
done

while脚本实现IP网段的循环ping通

[root@centos7 ~]# cat  ip1.sh      # while脚本
#!/bin/bash

IP=192.168.0
NUM=1

while [ $NUM -lt 255 ];do

  ping -c1 -W1 $IP.$NUM &> /dev/null

  if [ $? -eq 0 ];then
     echo "$IP.$NUM success!"
  else
     echo "$IP.$NUM fail!"
  fi

  let NUM++

done

#  chmod +x ip1.sh   # 加上执行权限
# bash  ip1.sh 执行脚本

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

(1)新建一个backup目录

[root@centos7~]#mkdir backup

(2)修改执行脚本并加上执行权限

[root2centos7~]vim back_etc.sh
tar  Jcf  /root/backup/etcbak-`date -d '-1 day' +%F-%H`.tar.xz  /etc/  &> /dev/null  写入脚本内容
chmod +x back_etc.sh  加上执行权限

(3)写入计划任务

[root@centos7~]#crontab -e
30  1   *  *  1-5  /root/back_etc.sh

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

(1)先将要执行的内容写入到脚本中

[root@centos7data]#vim wall.sh

#!/bin/bash
#
#********************************************************************
#Author: liudalao
#QQ: 77421225
#Date: 2019-11-24
#FileName: wall.sh
#URL: http://www.struggle.com
#Description: The test script
#Copyright (C): 2019 All rights reserved
#********************************************************************
[ `df |grep /dev/sd | sed -nr 's/.* ([0-9]+)%.*/1/p' | sort -nr |head -1` gt 80 ] && wall disk will be full  将执行的结果不在屏幕上显示,直接存放到垃圾箱里

(2)将脚本加执行权限

[root@centos7data]#chmod +x wall.sh

(3)然后在计划任务中执行脚本即可

[root@centos7~]#crontab -e

*/10  *  *  *  0 /data/wall.sh

  

原文地址:https://www.cnblogs.com/struggle-1216/p/11924767.html