Linux Process Memory Usage

本文提供了一个Shell脚本示例,用于监控Linux操作系统中特定进程的内存使用情况,每2秒记录一次数据,并将结果保存到CSV文件中。脚本通过检查进程ID来确定目标进程是否仍在运行。

http://www.linuxforums.org/forum/red-hat-fedora-linux/49630-how-programmatically-monitor-process-memory-usage.html

http://opentips.blogspot.com/2008/01/linux-process-memory-usage.html

The following shell script is used to monitor the memory usage of a process.
Save the following script in a file (for ex, memusage.sh) and run it with the process name you want to monitor.

for ex, ./memusage.sh myapp
Now the script log the mem usage in memusage.csv file for every 2 secs. You can change the time period by change the PERIOD value in the script.

#!/bin/sh
#GetmemUsageModified2
USAGE="Usage: $0 processName"
if [ $# -ne 1 ]; then
echo $USAGE
exit 1
fi
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PID=""
while :
do
PROCESS_PID=`/sbin/pidof $1`
if [ "$PROCESS_PID.X" != ".X" ]; then
break
fi
done
LOG_FILE="memusage.csv"
echo "ElapsedTime,VmSize,VmRSS" > $LOG_FILE
ELAPSED_TIME=`date`
PERIOD=2 # seconds
while :
do
if [ -d /proc/$PROCESS_PID ] ; then
VM_SIZE=`awk '/VmSize/ {print $2}' < /proc/$PROCESS_PID/status`
if [ "$VM_SIZE.X" = ".X" ]; then
continue
fi
VM_RSS=`awk '/VmRSS/ {print $2}' < /proc/$PROCESS_PID/status`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
echo "$ELAPSED_TIME,$VM_SIZE,$VM_RSS" >> $LOG_FILE
sleep $PERIOD
VM_SIZE=""
VM_RSS=""
ELAPSED_TIME=`date +%H:%M:%S:%N`
else
echo "$1 is no longer a running process"
exit 0
fi
done

 

转载于:https://www.cnblogs.com/androidme/archive/2013/04/05/3001250.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值