Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

out TOP to a file..

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
0
0
US
Hi,

I need to monitor the memory used by a process that I can see through the TOP command. I need to monitor this every 5 mins. Is there a way I can write a shell script that can accomplish this task..record the memory usage by a process and store it in a file every 5 mins?

pat
 
Why not simply a cron job ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Something like...
Code:
#!/bin/ksh

# PID of process to monitor
PID=1234
LOG=memory.log

while (( 1 ))
do
    date >> ${LOG}
    pmap ${PID}|grep total >> ${LOG}
    (( $? )) && sleep 1200 || break
done
 
You can have a similar command to top using the /usr/ucb/ps command. here are examples to show top CPU usage and top Mem usage processes

#########################################
echo "----- Top 10 Processes By Usage of Processor(s)"
/usr/ucb/ps -aux | awk '{print $3 " " $2 " " $1 " " $11}' | grep '%'

/usr/ucb/ps -aux | awk '{print $3 " " $2 " " $1 " " $11}' | sort -r | grep -v '%MEM' | head -10

#########################################
echo "----- Top 10 Processes By Usage of Memory "
echo
/usr/ucb/ps -aux | awk '{print $4 " " $2 " " $1 " " $11}' | grep '%'

/usr/ucb/ps -aux | awk '{print $4 " " $2 " " $1 " " $11}' | sort -r | grep -v '%MEM' | head -10
#########################################

You can put these in scripts and then output to file in cronjob

Hope this helps
 
What version of *nix?

if AIX

nohup vmstat 600 > /tmp/filename &

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Thanks all.
The code that SamBones jotted down..seemed to the closed I was lookin for. Only problem was when I tried that the script sayd pmap not found.

What is pmap for?

pat
 
It's a Solaris utility that gives you the memory used by a process. I don't know if it's on any other *nix.

If you're using Solaris, try this to look for it...
Code:
find / -name pmap 2>/dev/null
If not, maybe change it to soemthing like...
Code:
#!/bin/ksh

# PID of process to monitor
PID=1234
LOG=memory.log

while (( 1 ))
do
    date >> ${LOG}
    ps -o vsz,rss,comm -f -p ${PID}|tail -1 >> ${LOG}
    (( $? )) && sleep 1200 || break
done
That will give you the total size of the process (vsz = virtual size) and the amount that is currently in memory (rss = resident set size). Some of it will be swapped out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top