Memory usage of a process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Memory usage of a process
# 1  
Old 09-02-2008
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
# 2  
Old 09-02-2008
Assuming 200000 is more correct than 300000,

Code:
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.
# 3  
Old 09-02-2008
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
# 4  
Old 09-02-2008
Quote:
Originally Posted by tafil
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..
# 5  
Old 09-02-2008
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
# 6  
Old 09-02-2008
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
# 7  
Old 09-03-2008
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

Code:
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


Last edited by era; 09-03-2008 at 03:01 AM.. Reason: Useless Use of grep | awk | awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Find memory usage for a process

I have multiple oracle databases on one server. All the database are running from the same user i.e. oraent. The process for each database can be distinguished by the ps -ef command Eg : ps -ef | grep oraentThe Output : oraent 5361 1 0 20:58:05 ? 0:00 oracledb1... (11 Replies)
Discussion started by: yashreads
11 Replies

2. Solaris

Process memory usage

hi all gurus: I want to find out Solaris process memory usage, but got a bit confused, see if any one can provide me some guidance. i tend to use prstat -a to get total memory consumption by user (I know prstat likely have a bug that simply sum up the memory, regardless if the memory being... (5 Replies)
Discussion started by: oakville
5 Replies

3. HP-UX

Virtual Memory Usage a Process

Hi all, Is there any command which shows the virtual memory usage of a particular process in HP-UX machine. I have tried with ps, top but could not get what I want. Kindly provide me a solution. Thanks in Advance ARD (4 Replies)
Discussion started by: ard
4 Replies

4. UNIX for Advanced & Expert Users

collecting memory usage by a process

Hi Guys, I work on a AIX environment and I'm trying to write a script where I can collect all the memory used by a process. Basically I'm executing the command 'ps -fu userid' to get all the process ids and then executing the 'ps v PID' to get all the memory allocated by PPID. My question is... (2 Replies)
Discussion started by: arizah
2 Replies

5. Emergency UNIX and Linux Support

Memory usage of a process having shared libraries

Hi, I have the following two processes that's built with static libraries. Both the process have many common libraries. -rwxr-xr-x 1 xxx xxx 152946280 Oct 15 08:38 server1 -rwxr-xr-x 1 xxx xxx 41633880 Oct 15 08:39 server2. I built these two server processes making all the... (1 Reply)
Discussion started by: srivatsan_vn
1 Replies

6. Programming

Memory usage of a process having shared libraries

Hi, I have the following two processes that's built with static libraries. Both the process have many common libraries. -rwxr-xr-x 1 xxx xxx 152946280 Oct 15 08:38 server1 -rwxr-xr-x 1 xxx xxx 41633880 Oct 15 08:39 server2. I built these two server processes making all the... (1 Reply)
Discussion started by: srivatsan_vn
1 Replies

7. AIX

How to trace cpu/memory usage for a process

I don't know when the process will start and end, I need write a script to trace it's cpu/memory usage when it is runing. How to write this script? (2 Replies)
Discussion started by: rainbow_bean
2 Replies

8. HP-UX

how could I get a process Memory Usage

I use pstat API to get Process Infomation I would like to get a process 1.process owner 2.how many physical memory and virtual memory and total memory used(KB) and usage(%) 3.a process excution file create time 4.a process excution file access time I do't know which attribute it i need ... (3 Replies)
Discussion started by: alert0919
3 Replies

9. Solaris

RAM Physical Memory usage by each Process.

Hi All, I am trying to find the physical memory usage by each process/users. Can you please let me know how to get the memory usage?. Thanks, bsraj. (12 Replies)
Discussion started by: bsrajirs
12 Replies

10. UNIX for Advanced & Expert Users

how to restrict memory usage by a process

we are running red hat ES4 and i would like to know if there is anyway of restrcting the maximum amount of memory that a process can get? I have a single preocess that is taking >13GB. Thanks, Frank (4 Replies)
Discussion started by: frankkahle
4 Replies
Login or Register to Ask a Question