Script to Monitor a Process with Top.

Hi,

I have written a script to monitor a Process with the help of top command. This is my script.

======================

#!/bin/sh

DATE=`date +%Y%m%d%H%M%S`
HOME=/home/xmp/testing/xmp_report
RADIUS_PID=`xms -xmp sh pr | grep "RADIUS.iamsp02ldv" |awk '{ print $3 }'`
PSE_PID=`xms -xmp sh pr | grep "PRESENCE-STORE.iamsp02ldv" |awk '{ print $4 }'`


printf "\n\n############## Process Info. #############\n\n" >> $HOME.$DATE
top -b -p $RADIUS_PID -n 1 >> $HOME.$DATE

printf "\n\n############## CPU Info. PSE  #############\n\n" >> $HOME.$DATE
top -b -p $PSE_PID -n 1 >> $HOME.$DATE

exit 0

==========================
The out put is as below

############## Process Info. #############

top - 23:13:27 up 312 days, 10:22,  3 users,  load average: 1.41, 1.80, 1.98
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s): 15.4% us,  3.2% sy,  1.4% ni, 79.9% id,  0.1% wa,  0.0% hi,  0.0% si
Mem:  16631940k total, 15986148k used,   645792k free,    64420k buffers
Swap: 12288952k total,   962888k used, 11326064k free,  4281764k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
30700 xmp       15   0 2986m 2.9g 1488 S  0.0 18.3 173:33.06 cee



############## Info  #############

top - 23:13:28 up 312 days, 10:22,  3 users,  load average: 1.41, 1.80, 1.98
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s): 15.4% us,  3.2% sy,  1.4% ni, 79.9% id,  0.1% wa,  0.0% hi,  0.0% si
Mem:  16631940k total, 15986276k used,   645664k free,    64420k buffers
Swap: 12288952k total,   962888k used, 11326064k free,  4281764k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11329 xmp       16   0  153m 144m 2160 S  2.0  0.9  14:58.60 cee

================================================================
Though it is fine i want to append the same with the date before this. The date is there on the file name but i want the same inside also. It should look something like this.

 DateP                     ID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
2010122023132411329 xmp       16   0  153m 144m 2160 S  2.0  0.9  14:58.60 cee

Any help would be appreciated.

Regards,
Siddh

You may add following just before "exit 0"

 
awk -v d=$DATE '{if($1=="PID"){ print "Date",$0;a=1;}else{if(a=="1"){print d,$0;a=0;}else print;}}' $HOME.$DATE > temp
mv temp $HOME.$DATE

Hi,

Thanks for the reply. Here is what the error that is recieved.

mv command line was not correct. Pls try now.

Hi,

Thanks a lot. You saved my day. It working exactly the way in wanted.

Regard,
Siddhesh

thru sed..

sed "/ PID/s//Date\t\tPID/;/Date\t\tPID/{N;s/\n/\n$(date '+%Y%m%d%H%M%S') /}" HOME.$DATE > temp
mv temp $HOME.$DATE