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…

Squarespace Image Export

To gain continued access to your Squarespace website images after cancelling your subscription you have several...

MySQL 8.x GRANT ALL STATEMENT

-- CREATE CREATE USER 'tgnrestoreuser'@'localhost' IDENTIFIED BY 'AppleSauceLoveBird2024'; GRANT ALL PRIVILEGES ON...

Exetel Opt-Out of CGNAT

If your port forwards and inbound and/or outbound site-to-site VPN's have failed when switching to Exetel due to their...