摘要 腾兴网为您分享:二个linux下查看内存使用情况的shell脚本(),政务易,悦作业,优化大师,王者荣耀等软件知识,以及单向历app,优路教育app,kimoji,开关电源设计软件,皮皮高清影视,lol防封,公卫人论坛,电子琴软件,问卷调查软件,惠普m1536dnf驱动,蓝蚂蚁,pp体育tv版,小型仓库软件,时间管理四象限,培优教育等软件it资讯,欢迎关注腾兴网。在linux中,可以通过查看文件/proc/$PID/status以获取指定进程资源(内存,堆栈,信号量,子父进程)的使用情况。 以下脚本的实现,就是通过查看status文件中VmRSS的字段来看系统或进程使...
在linux中,可以通过查看文件/proc/$PID/status以获取指定进程资源(内存,堆栈,信号量,子父进程)的使用情况。
以下脚本的实现,就是通过查看status文件中VmRSS的字段来看系统或进程使用的内存情况。
代码如下:
复制代码 代码示例:
#!/bin/bash
#filename chk_memory.sh
#edit by www.jquerycn.cn
echo -n "PidName " > /tmp/pid.f
echo "MeMery " >> /tmp/pid.f
PIDPATH=/proc/
TOTLE=0
cd $PIDPATH
DOC=`ls`
for DOCNAME in $DOC
do
if [ -d $DOCNAME ]; then
cd $DOCNAME
if [ -f status ];then
MEMERY=`grep VmRSS status | awk -F':' '{print $2}' | awk '{print $1}'`
PIDNAME=`grep Name status | awk -F':' '{print $2}'`
if [ $MEMERY ]; then
echo -n $PIDNAME >> /tmp/pid.f
echo -n " " >> /tmp/pid.f
echo $MEMERY >> /tmp/pid.f
TOTLE=`expr $TOTLE + $MEMERY`
cd ..
else
cd ..
fi
else
cd ..
fi
fi
done
TOTLE=`expr $TOTLE / 1024`
echo "TOTLE USERD: $TOTLE MB" >> /tmp/pid.f
cat /tmp/pid.f
调用示例,如下图:
上图展示了部分结果,限于篇幅,仅提供以上截图。
2,统计本机内存情况和使用状况的脚本
复制代码 代码示例:
#!/bin/bash
# File Name: checkMem.sh
#fetch and process memory information
[ -f /proc/meminfo ] && { #First judge whether the file exists
#-e是模式的意思,常用来保护以破折号开头的模式。
#-w是全字匹配。
#-i忽略大小写。
#-d指定分割符,-f为按照分割符取出的域,2-指的是域2及以后
Buffers=`grep -we 'Buffers' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
Cached=`grep -we 'Cached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemFree=`grep -ie 'MemFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemTotal=`grep -ie 'MemTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapCached=`grep -ie 'SwapCached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapFree=`grep -ie 'SwapFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapTotal=`grep -ie 'SwapTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
}
MEMUSED="$(( ( ( ( $MemTotal - $MemFree ) - $Cached ) - $Buffers ) / 1024 ))"
MEMTOTAL="$(( $MemTotal / 1024))"
MEMFREE="$(( $MEMTOTAL - $MEMUSED ))"
MEMPER="$(( ( $MEMUSED * 100 ) / $MEMTOTAL ))"
[ "$SwapTotal" -gt "1" ] && {
SWAPUSED="$(( ( ( $SwapTotal - $SwapFree ) - $SwapCached ) / 1024 ))"
SWAPTOTAL="$(( $SwapTotal / 1024))"
SWAPFREE="$(( $SWAPTOTAL - $SWAPUSED ))"
SWAPPER="$(( ( $SWAPUSED * 100 ) / $SWAPTOTAL ))"
} || {
SWAPUSED="0"
SWAPTOTAL="0"
SWAPPER="0"
}
# display the information
/bin/echo
/bin/echo "Memory"
/bin/echo "Used: $MEMUSED MB"
/bin/echo "Free: $MEMFREE MB"
/bin/echo "Total: $MEMTOTAL MB"
/bin/echo
/bin/echo "Swap"
/bin/echo "Used: $SWAPUSED MB"
/bin/echo "Free: $SWAPFREE MB"
/bin/echo "Total: $SWAPTOTAL MB"
/bin/echo
调用示例,如下图:
相关推荐