zoukankan      html  css  js  c++  java
  • 检查服务存在脚本|cpu_memcache_disk使用情况脚本

    service_check.sh

    #! /bin/bash
    
    list="nginx redis java rabbit kafka ssh"
    
    for i in $list;
    do
    wci=`ps -ef | grep "$i" | grep -v grep | wc -l`
    if [ $wci -gt 0 ]; then echo $i $wci >> service_check.out; fi
    done

    cpu_mem_disk.sh

    #!/bin/bash
    #CHECK CPU
    cpu_total=`cat /proc/cpuinfo | grep processor |wc -l`
    
    echo "cup_total:$cpu_total"
    
    #CHECK MEMORY
    
    mem_total=`free -m | sed -n '2p' | awk '{print $2}'`
    echo "mem_total:$mem_total"
    
    #CHECK DISK
    
    # disk_total=`df -l | awk '{print $2}' | sed -e 1d | awk '{sum += $1} END {print sum/1024/1024"GB"}' `
    disk_total=`df -l | awk '{print $2}' | sed -e 1d | awk '{sum += $1} END {print sum/1024/1024}' `
    echo "disk_total:$disk_total"

     awk求和

    简单按列求和

    [linux@test /tmp]$ cat test

    123.52
    125.54
    126.36

    [linux@test /tmp]$ awk '{sum += $1};END {print sum}' test

    375.42

    对符合某些条件的行,按列求和

    [linux@test /tmp]$ cat test

    aaa 123.52
    bbb 125.54
    aaa 123.52
    aaa 123.52
    ccc 126.36

    对文件test中 第一列为aaa的行求和

    [linux@test /tmp]$ awk '/aaa/ {sum += $2};END {print sum}' test

    370.56

    send相关

    $ sed -e 4a
    ewline testfile #使用sed 在第四行后添加新字符串  
    HELLO LINUX! #testfile文件原有的内容  
    Linux is a free unix-type opterating system.  
    This is a linux testfile!  
    Linux test  
    newline

    将 /etc/passwd 的内容列出并且列印行号,同时,请将第 2~5 行删除!

    [root@www ~]# nl /etc/passwd | sed '2,5d'
    1 root:x:0:0:root:/root:/bin/bash
    6 sync:x:5:0:sync:/sbin:/bin/sync
    7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    .....(后面省略).....

    sed 的动作为 '2,5d' ,那个 d 就是删除!因为 2-5 行给他删除了,所以显示的数据就没有 2-5 行罗~ 另外,注意一下,原本应该是要下达 sed -e 才对,没有 -e 也行啦!同时也要注意的是, sed 后面接的动作,请务必以 '' 两个单引号括住喔!

    在第二行后(亦即是加在第三行)加上『drink tea?』字样!

    [root@www ~]# nl /etc/passwd | sed '2a drink tea'
    1 root:x:0:0:root:/root:/bin/bash
    2 bin:x:1:1:bin:/bin:/sbin/nologin
    drink tea
    3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
    .....(后面省略).....

    那如果是要在第二行前

    nl /etc/passwd | sed '2i drink tea' 

    如果是要增加两行以上,在第二行后面加入两行字,例如 Drink tea or ..... 与 drink beer?

    [root@www ~]# nl /etc/passwd | sed '2a Drink tea or ......
    > drink beer ?'
    1 root:x:0:0:root:/root:/bin/bash
    2 bin:x:1:1:bin:/bin:/sbin/nologin
    Drink tea or ......
    drink beer ?
    3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
    .....(后面省略).....

    每一行之间都必须要以反斜杠『 』来进行新行的添加喔!所以,上面的例子中,我们可以发现在第一行的最后面就有 存在。

    搜索 /etc/passwd有root关键字的行

    nl /etc/passwd | sed '/root/p'
    1  root:x:0:0:root:/root:/bin/bash
    1  root:x:0:0:root:/root:/bin/bash
    2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh
    3  bin:x:2:2:bin:/bin:/bin/sh
    4  sys:x:3:3:sys:/dev:/bin/sh
    5  sync:x:4:65534:sync:/bin:/bin/sync
    ....下面忽略 

    如果root找到,除了输出所有行,还会输出匹配行。

    使用-n的时候将只打印包含模板的行。

    删除/etc/passwd所有包含root的行,其他行输出

    nl /etc/passwd | sed  '/root/d'
    2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh
    3  bin:x:2:2:bin:/bin:/bin/sh
    ....下面忽略
    #第一行的匹配root已经删除了

    sed替换

    sed 's/要被取代的字串/新的字串/g'

    将 IP 前面的部分予以删除

    [root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
    192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

    流程控制

    if else

    fi

    if 语句语法格式:

    if condition
    then
        command1 
        command2
        ...
        commandN 
    fi

    写成一行(适用于终端命令提示符):

    if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi

    if else

    if else 语法格式:

    if condition
    then
        command1 
        command2
        ...
        commandN
    else
        command
    fi

    if else-if else

    if else-if else 语法格式:

    if condition1
    then
        command1
    elif condition2 
    then 
        command2
    else
        commandN
    fi

    以下实例判断两个变量是否相等:

    a=10
    b=20
    if [ $a == $b ]
    then
       echo "a 等于 b"
    elif [ $a -gt $b ]
    then
       echo "a 大于 b"
    elif [ $a -lt $b ]
    then
       echo "a 小于 b"
    else
       echo "没有符合的条件"
    fi

    输出结果:a 小于 b

    if else 语句经常与 test 命令结合使用,如下所示:

    num1=$[2*3]
    num2=$[1+5]
    if test $[num1] -eq $[num2]
    then
        echo '两个数字相等!'
    else
        echo '两个数字不相等!'
    fi

    输出结果:两个数字相等!

    Shell 变量

    使用shell变量

    name=tom
    
    echo "$name"  
    echo $name
    echo '$name'
  • 相关阅读:
    python相关软件安装流程图解————————pycharm安装——————pycharm-professional-2018.3.1
    爬虫——python——百度地图经纬度查询——经纬度查看地点地名——利用百度API获取地名经纬度——爬取所有的中国地址
    博客园自定义美化——博客园公告功能(公告栏圆形时钟)——博客园公告栏圆形时钟功能
    博客园自定义美化——博客园页脚功能(底部简介关注打赏)——博客园文章个性签名功能
    博客园自定义美化——博客园页首功能(右侧悬浮打赏)——右侧添加悬浮打赏功能
    Django项目:CMDB(服务器硬件资产自动采集系统)--12--08CMDB采集硬件数据日志记录
    Django项目:CMDB(服务器硬件资产自动采集系统)--11--07CMDB文件模式测试采集硬件数据
    centos 安装gitlab
    vscode 配置
    mssql下调用dll
  • 原文地址:https://www.cnblogs.com/soymilk2019/p/14890364.html
Copyright © 2011-2022 走看看