The http://pastie.org/1264823 is no longer valid link
]]>Wow this seems old now. Cringing at my lack of bash (and Linux) skills.
Kernc, the -o option tells you this, the first column is “%mem”, i.e %age of memory used. Now I would probably do ps -eo %mem,pid,user,args | sort -rn, so that the processes are sorted in order of memory usage. And of course pipe it straight to mail as suggested by poisonbit rather than attach a text file with mutt.
]]>I’ve used mutt too, as I don’t know how to use mailutils. However in my server I have a laptop HDD installed in the name of noise, whenever memory is running low and it uses swap, everything slows down a lot, so I have a warning system for when memory is being used.
#!/bin/bash
memfree=”`cat /proc/meminfo | grep MemFree | cut -d: -f2 | cut -dk -f1`”
memfreemb=$(echo “scale=5; $memfree/1024” | bc -l| awk -F. ‘{if(($2/10^length($2)) >= .5) printf(“%d.0n”,$1+1);else printf(“%d.0n”,$1)}’)
if [ $memfree -lt 204800 ]; then
ps -eo %mem,pid,user,args >/tmp/processes.txt
echo ‘Warning, free memory is ‘$memfreemb’mb’ | mutt -a /tmp/processes.txt -s “Server Alert” — [email protected]
fi
Cheers, your version is definitely an improvement!
]]>mmm it seems that it is wordpress that eats the code, if you want to edit the first comment, it should look like this: http://pastie.org/1264823
]]>#!/bin/bash
LANG=C
free=$(free -mt | awk '/Total/{print $4}')
[ $free -lt 256 ] &
]]>Just a few tips for the next version:
free -mt | grep Total | awk ‘{print $4}’
is the same that:
free -mt | awk ‘/Total/{print $4}’
Also when I parse commands output, I use to set LANG=C into the script, to avoid locales (“Total” is not said “Total” in all languages)
And in this case, we can avoid the mutt depend to attach files, and insert the txt into the mail body:
#!/bin/bash
LANG=C
free=$(free -mt | awk '/Total/{print $4}')
[ $free -lt 256 ] &mb"
procs="$(ps -eo %mem,pid,user,args)"
printf '%sn%s' "$msg" "$procs" | mail -s "Server alert" [email protected]
}
Greetings :)
]]>