1

My host is offering me a limited amount of RAM for my websites and I would like to monitor the memory consumption by running a script in the cron.

I have made up this command:

ps -u xxxxxx -o rss,command | grep -v peruser | awk '{sum+=$1} END {print sum/1024}'

But the way my host is calculating memory has a particularity. It ignores processes that runs for less than 5 minutes.

Do you know of a command or Python script that can show me the total used memory ignoring short lived processes?

0

1 Answer 1

1

Use etime to find the elapsed time of a process. You can then modify your awk command to check if this time is greater than 5 mins.

ps -u xxxxxx -o rss,etime,command | ...

The format of elapsed time is [[dd-]hh:]mm:ss. This means the time could be 00:30, 12:57:39 or even 4-08:27:12. You can use awk to parse this time format and convert it into seconds. Once in seconds, check if it is more than 5 minutes, and if so add it to the running total of rss.

Here is a sample script which does this:

ps -u xxxxxx -o rss,etime,command | grep -v peruser | grep -v RSS | sed 's/^ *//g' | awk '{
 split($2,arr,":") #split time on :
 len=0
 for(i in arr) len++ 
 secs=arr[len]+60*arr[len-1] #convert to seconds
 if(len>2) secs=secs+substr(arr[len-2],length(arr[len-2])-1)*60*60

 if(secs>5*60) { #check if more than 5 minutes
     print $0 #print out the process
     sum+=$1 
 }
} END{print "SUM:"sum/1024}'
2
  • I really suck at unix commands :( Can you please tell me how to modify my awk command and filter out processes with a an etime of 5 minutes or less? Thank you!
    – Vallieres
    Sep 15, 2011 at 13:27
  • Damnit, I did not get the notification of your answer. I'll test you script right away and post my results tomorrow. Thanks dogbane!
    – Vallieres
    Sep 23, 2011 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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