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 *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…