Could you please help me to sort this out? I'm trying to write a bash script which will monitor ram usage (not only ram but cpu and network as well) and show me 3 processes which use the most ram in past X seconds? I know that I can find which processes use the most ram by running the following:

ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 3

But how can this be repeated? I want to see not the situation at this exactly second, but monitor the situation for (let it be) 60 seconds and see top 3 processes which was using the most ram.

  • You can try watch "ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 3", you can look into using top -b` and passing it additional sorting parameters, and there are also other tools like sar and vmstat that might be helpful. – DopeGhoti Apr 15 '16 at 22:45

I use the dstat command. Very useful. Explore it using man dstat

For example:

Find most expensive in terms of memory every 3 seconds:

#dstat --top-mem 3 --most-expensive- memory process firefox 1430M firefox 1430M firefox 1431M firefox 1435M firefox 1435M firefox 1435M firefox 1435M firefox 1438M

Similarly you can dot it for cpu , network , IO , etc.

dstat --top-cpu 3 -most-expensive- cpu process
firefox 3.5 firefox 4.5 firefox 2.4 firefox 2.6 firefox 2.8 firefox 2.0 firefox 2.8


Top 3 memory hungry processes command:

ps auxxx --sort=-rss | head -4

Top 3 cpu hungry processes:

ps auxxx --sort=-%cpu | head -4

You can run these scripts through Cronjob at every minute.

          • /tmp/serverhealth.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.