#!/bin/bash
USER=test
list=(openvpn docker mysql redis)
l=${list[*]}
for i in $l
do
eval RSS_$i=$(ps -aux | grep ${USER} | grep $i | grep -v grep | awk '{print $6}' | awk 'BEGIN{a=0}{a=a+$1}END{print a}')
eval echo \${RSS_$i}
eval RSS_$i_G=$(echo $(eval echo \${RSS_$i})/1024/1024 | bc)
eval echo \${RSS_$i_G}
if eval [[ \${RSS_$i_G} -gt 12 ]]
then
echo "WARNING: MEMORY used by $i is more than 12G"
elif eval [[ \${RSS_$i_G} -lt 1 ]]
then
echo "MEMORY used by $i is less than 1G"
else eval echo "MEMORY used by $i is \${RSS_$i_G}G"
fi
done
fi
done
上面是一个shell脚本,用来统计test用户运行的4个服务占用的内存RSS值,超过阈值触发警告。
1.赋值时使用eval
2.输出变量时变量前加\
3.给出了变量计算的一种方式