[shell]监控剩余内存

free-mem-monitor.sh

#!/bin/sh

interval=1

[ ! “$1” == “” ] && interval=$1

mem=$(free | grep “Total” | awk ‘{print $4}’)

max=$mem && t_max=$(date “+%H:%M:%S”)

min=$mem && t_min=$(date “+%H:%M:%S”)

while true; do

        new=$(free | grep “Total” | awk ‘{print $4}’)

        delta=$(expr $new – $mem)

        [[ $new -gt $max ]] && max=$new && t_max=$(date “+%H:%M:%S”)

        [[ $new -lt $min ]] && min=$new && t_min=$(date “+%H:%M:%S”)

        now=$(date “+%H:%M:%S”)

        d_max=$(expr $max – $mem)

        d_min=$(expr $min – $mem)

        printf “%s Delta = %4d K  min = %4d K (%s) max = %4d K (%s)\n” $now $delta $d_min $t_min $d_max $t_max

        sleep $interval

done

output:

# sh free-mem-monitor.sh 5

02:02:23 Delta =  -12 K  min =  -12 K (02:02:23) max =    0 K (02:02:23)

02:02:28 Delta =  36 K  min =  -12 K (02:02:23) max =  36 K (02:02:28)

02:02:33 Delta =  36 K  min =  -12 K (02:02:23) max =  36 K (02:02:28)

02:02:39 Delta = -188 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:02:44 Delta =  12 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:02:49 Delta =  12 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:02:54 Delta =  12 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:02:59 Delta =    0 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:03:04 Delta =    0 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:03:09 Delta =    0 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:03:14 Delta =  -12 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:03:20 Delta = -180 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:03:25 Delta =  -12 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:03:30 Delta =  -24 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:03:35 Delta =  -24 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

02:03:40 Delta =  -24 K  min = -188 K (02:02:39) max =  36 K (02:02:28)

    原文作者:老郭
    原文地址: https://www.jianshu.com/p/5fad017f6e75
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞