Script to check memory usage every second on Linux and write to a log file

Written by James McDonald

August 30, 2011

#!/bin/bash

###
### Sample background worker script
### for linuxquestions.org written
### by Florian Harbich (user doc.nice)
###
### Free for use or modification, even if
### useless in this variant...
###

# to terminate the script 
# run "touch /var/run/backgroundworker.ctl"
# as root.

TERMINATORFILE=/var/run/backgroundworker.ctl
GOON=1

# the original while [ $GOON ] didn't work
# for me so changed to [ $GOON -ne 0 ]
while [ $GOON -ne 0 ]; do
	[ -f "$TERMINATORFILE" ] && GOON=0 
#	echo $GOON

        # do your repeated stuff here
	LOG=/home/myuser/free.log

        # date time formatted as 
        # YYYYMMDD HH:MM:SS
	DT=`date +"%Y%m%d %H:%M:%S"`

        # this gets the _used_ memory
	FR=`free -m | sed -n '2 p' | awk '{print $3}'`  

	echo $DT $FR >> $LOG

	
	sleep 1
done
rm -f "$TERMINATORFILE"

To do: Daemonize it

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...