Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Tuesday, 30 June 2009

Tip of the Day: Make a Memory Usage Script

Here's a simple memory usage script made in Bash, which will use the parsed output of free -tom utility. First, here is what the -tom argument stands for:

-t displays a line containing the total amount of memory (physical memory + swap)
-o disables the display of a buffer line
-m displays sizes in MB

Here goes the script, which outputs the total, used and free memory:

#!/bin/bash

# total memory
memt=$(free -tom | grep "Total:" | awk '{print $2}')
# used memory
memu=$(free -tom | grep "Total:" | awk '{print $3}')
# free memory
memf=$(free -tom | grep "Total:" | awk '{print $4}')
echo "Total memory: $memt MB"
echo "Used memory: $memu MB"
echo "Free memory: $memf MB"

You can put this in a file of your choice, say memory.sh, make it executable (chmod 755 memory.sh), put it inside ~/bin (or a directory of your choice) and then include that directory in your $PATH, so you'll be able to run the script just by typing memory.sh.