How to find the memory consumption of a particular process in linux for every 5 seconds

自古美人都是妖i 提交于 2019-12-03 02:58:14

you may use SNMP to get the memory and cpu usage of a process in a particular device in network :)

Requirements:

  • the device running the process should have snmp installed and running
  • snmp should be configured to accept requests from where you will run the script below(it may be configured in snmpd.conf)
  • you should know the process id(pid) of the process you want to monitor

Notes:

  • HOST-RESOURCES-MIB::hrSWRunPerfCPU is the number of centi-seconds of the total system's CPU resources consumed by this process. Note that on a multi-processor system, this value may increment by more than one centi-second in one centi-second of real (wall clock) time.

  • HOST-RESOURCES-MIB::hrSWRunPerfMem is the total amount of real system memory allocated to this process.

**

Process monitoring script:

**

echo "IP: "
read ip
echo "specfiy pid: "
read pid
echo "interval in seconds:"
read interval

while [ 1 ]
do
    date
    snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfCPU.$pid
    snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfMem.$pid
    sleep $interval;
done

Use top -p PID where PID is the process ID. Information about the process should be displayed, including percent of system memory used. Type d and an integer in seconds to change the refresh rate.

use watch to periodically execute your script. here is an example:

watch 'cat /proc/status' --interval=5

watch 'ps aux' --interval=5
mdunsmuir

This previously posted question:

How to measure actual memory usage of an application or process?

seems like it may thoroughly address your question.

edit: My personal favorite Linux utility for checking the resource usage of processes is top, though it can be misleading for reasons that are explained in the question I linked.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!