• 自我学习笔记08


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

    # -e UNIX选项
    # -o pid,ppid,cmd,%men,%cpu 显示pid,ppid 命令 内存和CPU的使用系统资源情况
    # --sort=-%men 以内存使用情况倒序方式排列
    ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
    

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

    # for循环
    
      1 #!/bin/bash
      2 #
      3 #********************************************************************
      4 #Author:        Ronald-wang
      5 #QQ:            xxx
      6 #Date:          2019-12-22
      7 #FileName:     scanip.sh
      8 #URL:           https://www.cnblogs.com/Ronald-wang/
      9 #Description:      The Scanip script
     10 #Copyright (C):     2019 All rights reserved
     11 #********************************************************************
     12 Col_G="e[32m"
     13 Col_R="e[31m"
     14 Col="e[0m"
     15 IP=$1[ ! -n "$1" ] && exit
     16 for i in {1..255};do
     17     {
     18     ping -c1 -W1 $IP.$i &>/dev/null
     19     [ `echo $?` -eq 0 ]&& echo -e "$Col_G $IP.$i $Col ping success" || echo -e "$Col_R $IP.$i $Col ping fail" 
     20     } &
     21 done
     22 wait
    
    # while循环
    
     1 #!/bin/bash
      2 #
      3 #********************************************************************
      4 #Author:        Ronald-wang
      5 #QQ:            xxx
      6 #Date:          2019-12-22
      7 #FileName:     scanip.sh
      8 #URL:           https://www.cnblogs.com/Ronald-wang/
      9 #Description:      The test script
     10 #Copyright (C):     2019 All rights reserved
     11 #********************************************************************
     12 Col_G="e[32m"
     13 Col_R="e[31m"
     14 Col="e[0m"
     15 IP=$1
     16 [ ! -n "$IP" ] && exit 
     17 i=1
     18 while [ $i -lt 255 ];do
     19    {
     20    ping -c1 -W1 $IP.$i &>/dev/null
     21     if [ `echo $?` -eq 0 ];then 
     22         echo -e "$Col_G $IP.$i $Col ping success"
     23     else
     24         echo -e "$Col_R $IP.$i $Col ping fail" 
     25     fi
     26     } &
     27    let i++    
     28 done
     29 wait  
    
    

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

    [root@localhost scripts]#vim /etc/crontab 
      1 SHELL=/bin/bash
      2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
      3 MAILTO=root
      4 
      5 # For details see man 4 crontabs
      6 
      7 # Example of job definition:
      8 # .---------------- minute (0 - 59)
      9 # |  .------------- hour (0 - 23)
     10 # |  |  .---------- day of month (1 - 31)
     11 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
     12 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
     13 # |  |  |  |  |
     14 # *  *  *  *  * user-name  command to be executed
     15 30 13 * * 1-5 root /data/scripts/backup_etc.sh   
     
     # 编写脚本backup_etc.sh
     [root@localhost scripts]#vim backup_etc.sh 
      1 #!/bin/bash
      2 #
      3 #********************************************************************
      4 #Author:        Ronald-wang
      5 #QQ:            xxx
      6 #Date:          2019-12-12
      7 #FileName:     backup.sh
      8 #URL:           https://www.cnblogs.com/Ronald-wang/
      9 #Description:      backup /etc
     10 #Copyright (C):     2019 All rights reserved
     11 #********************************************************************
     12 #ColorStr="e[31m"
     13 #ColorEnd="e[0m"
     14 #echo -e "$ColorStr start backup! $ColorEnd"
     15 #sleep 2
     16 Back_Soudir='/etc' # 源备份地址
     17 Back_Desdir='/data/' # 目标备份路径
     18 BackDate=`date -d "1 day ago" +"%Y-%m-%d-%H"` # 目标备份文件名时间戳
     19 BackName='etcbak-'$BackDate # 目标备份文件名
     20 BackPath=$Back_Desdir$BackName # #目标备份全路径及基名                        
     21 tar -cpJ $Back_Soudir -f $BackPath.tar.xz &>/dev/null
     22 #echo -e "$ColorStr backup is finished!$ColorEnd"
    
     
     
    

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

    [root@localhost scripts]#vim /etc/crontab 
      1 SHELL=/bin/bash
      2 PATH=/sbin:/bin:/usr/sbin:/usr/bin
      3 MAILTO=root
      4 
      5 # For details see man 4 crontabs
      6 
      7 # Example of job definition:
      8 # .---------------- minute (0 - 59)
      9 # |  .------------- hour (0 - 23)
     10 # |  |  .---------- day of month (1 - 31)
     11 # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
     12 # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
     13 # |  |  |  |  |
     14 # *  *  *  *  * user-name  command to be executed
     15 */10 *  *  *  1-5 root df|awk -F\% '/^/dev/{print $1}'|awk '$NF>=80{print $1,$5}'
    
    
  • 相关阅读:
    投行风云:FO的酸甜苦辣【转】
    周末排毒
    LeetCode | Single Number II【转】
    Adding supplementary tables and figures in LaTeX【转】
    Algorithm | Tree traversal
    Leetcode | Linked List Cycle I && II
    Network | UDP checksum
    Ubuntu下将现有的文件打包成deb包
    Base64编码加密
    requestAnimationFrame 的原理与优势
  • 原文地址:https://www.cnblogs.com/Ronald-wang/p/12049397.html
Copyright © 2020-2023  润新知