Memory usage of a process

hi all,

i want to write a script that checks the memory usage of processes and send a mail with the name of the process witch is using more then 300mb RAM.
dose anybody have a sample script or an idea how i can make it ?

PROCCESSES="snmpd sendmail"

for myVar in $PROCCESSES
do
var1=$(ps -eo fname,rss | grep $myVar |awk '{print $2}'| awk '$1 > 200000')
echo $var1
if [ -n $var1 ]; then
echo "test1 mail later on"
else
echo "test2 mail later on"
fi
done

thanks in advance

Assuming 200000 is more correct than 300000,

ps -eo fname,rss | awk '$1 ~ /^(snmpd|sendmail)/ { if ($2 > 200000) print $1 }' >/tmp/boink
test -s /tmp/boink && mailx -s subject user@example.com </tmp/boink
rm /tmp/boink

Proper use of temporary file names etc left as an exercise.

thanks for your answer
this is a good idea to use temporary files
but how does the script know if there is something written on the temp file

test -s tests for both file existence and its size should be more than zero..

if this is a linux, you can check the /proc filesystem.

use "man proc" and search the explanation of what each numbered folder means, and what the data inside the file statm

Thank you very much for you help i finished the script.

This is teh final script

ps -eo fname,rss | grep -v RSS | awk '$2 > 350000' |awk '{print $1}' >/tmp/tempfile
test -s /tmp/tempfile &&
(
var1=`cat /tmp/tempfile`
echo HELO test.com
sleep 2
echo "mail from:<test@mail.com>"
sleep 2
echo "RCPT TO:<mailto@server.com>"
sleep 2
echo DATA
sleep 2
echo
echo These $var1 processes are taking more then 350MB RAM
echo please check it
echo .
echo
echo QUIT
sleep 2) | telnet 1.*.*.* 25
rm /tmp/tempfile

Doing the SMTP dialog on your own with telnet is error-prone and brittle; are you sure you're not better of passing the message to a proper MTA?

Just like in the Sendmail case, you should probably put in some headers in the message. Maybe your MTA accepts it without the headers, but they really are supposed to be required.

Note also the fix to the Useless Use of grep | awk | awk

ps -eo fname,rss | awk '!/RSS/{ if ($2 > 350000) print $1}' >/tmp/tempfile
test -s /tmp/tempfile &&
(
echo HELO test.com
sleep 2
echo "mail from:<test@mail.com>"
sleep 2
echo "RCPT TO:<mailto@server.com>"
sleep 2
echo DATA
sleep 2
cat <<HERE
From: test@mail.com
To: mailto@server.com
Subject: Processes taking more than 350MB

These processes are taking more than 350MB RAM.
Please check it.

HERE
cat /tmp/tempfile
echo .
echo QUIT
sleep 2) | telnet 1.*.*.* 25
rm /tmp/tempfile

Thank you very much for your help era, i am using SMTP telnet because i am very limited on the machine ( i can not configure the sendmail file).
do you know an example how to use MTA?