How to log memory usage per minute on Ubuntu Server?

Solution 1:

Install and configure the sysstat package, which does just what you're asking for.

sudo apt-get install sysstat

Solution 2:

If I were doing this I'd probably script something up with RRDtool.

RRDtool is something that is built for interval-based data-collection and then building graphs off the back of the data. If you've ever seen a netgraph on an ISPs control panel, it was probably done with RRDtool.

How you implement it is up to you. There are "pure" bash solutions with cron but there are also nice Python and Perl options floating around out there too:

  • https://github.com/icc/rrdtool-bash-scripts/blob/master/memory.sh

    As far as I can see, this updates an image in a set location. That could be on your webserver or scrobbled away over NFS into your homedir.

  • http://coreygoldberg.blogspot.co.uk/2008/05/python-rrdpy-round-robin-databases.html

    This holds its own timer and does pretty much whatever you like. I prefer this to bash because it's easier to poke around but whatever floats your boat. It's probably harder for a non-Pythoneer to get set up.

If you want a lot more than this, check out Munin. This uses RRDtool prolifically to log a whole load of system data and generates static HTML pages at a set interval.

Solution 3:

There is one more way to do this. By using cron jobs we can get this.

* * * * * username /path/to/./script.sh

the script is simply

#!/bin/bash
echo "$(date +%Y%m%d-%H%M%S) $(free -mt | awk 'END{print $4}')" >> ~/memlog.txt

before placing the script do

chmod +x script.sh

so create above script and do it as I said then place the script at /usr/bin location . then open your /etc/crontab with

sudo nano /etc/crontab

and place the first line there . That script executes for every minute and gives memory details for every minute .

So finally that log.txt file will hold the Memory log details for every minute.

and I have got one more useful information for you that

watch cat log.txt will show the log.txt file in terminal and it will update the content live.so in the current terminal it self with one time typing you can have live watch of the log.txt file.

Solution 4:

vmstat -S M 60 would do (in megabytes).

You only have to remember that actually free RAM is "free + cache + buf".

For logging purpose it might have sense to print it out time stamped which can be accomplished in various ways, for e. g.:

vmstat -nS M 60 | while read _vmstat; do echo $(date --rfc-3339=seconds) $_vmstat; done

Solution 5:

Here my trick. With this command I append reports of free, vmstat and top into txt files every 5s.

 watch -n 5 'free -mt  >> free-report.txt && vmstat -S M >> vm-report.txt && top -b -n1 >> top-report.txt'