logging memory usage of each process

Is there a way to log the amount of memory used by each process in linux? One of our servers has a memory usage spike that brings down the machine from time to time and the logging would really help us find the root cause.


Solution 1:

If your needs aren't very complex perhaps you could make do with ps and a while loop:

while true; do date; ps auxw --sort rss | tail -n 10; echo; sleep 60; done >> logfile

This logs the top ten processes (by memory use) every minute.

Solution 2:

The cleanest solution would be to put in a cron job which does something similar to

ps auxww | logger -p local0.info -t ps 

This would log ps output through your logging daemon to the local0 facility (can be changed to whatever you want)