Script to Monitor a Process with Top.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Monitor a Process with Top.
# 1  
Old 12-20-2010
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.


======================
Code:
#!/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

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

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


Moderator's Comments:
Mod Comment Please use code tags for sources and listings. You've got a PM on how to do that
# 2  
Old 12-20-2010
You may add following just before "exit 0"
Code:
 
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


Last edited by anurag.singh; 12-20-2010 at 06:47 AM..
# 3  
Old 12-20-2010
Hi,

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

Quote:
[xmp@iamsp02ldv ~]$ ./test.sh
mv: missing file argument
Try `mv --help' for more information.
# 4  
Old 12-20-2010
mv command line was not correct. Pls try now.
# 5  
Old 12-20-2010
MySQL

Hi,

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

Regard,
Siddhesh
# 6  
Old 12-20-2010
thru sed..
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Powershell script to monitor windows process

Hello Friend, I am not expert in power shell scripting. I nee custom powershell script which check if given process is running on windows machine or not. will use it in nagios to monitor that process. (0 Replies)
Discussion started by: ghpradeep
0 Replies

2. Shell Programming and Scripting

Shell script to monitor process with high CPU

Hi, Linux redhat 5.5 I need to write a kshell script that shows all the process that consume 100% CPU (or more. strange but there are time that top shows higger value that 100) and they are active more than 5 minute. The top command shows all the relevat information: The PID of the cpu ,... (0 Replies)
Discussion started by: yoavbe
0 Replies

3. Shell Programming and Scripting

Script to monitor the process

Hi, I need help to monitoring a process using the shell script The same output is below oracle 32578 32577 0 Feb27 ? 00:06:47 java -cp .:lib/ant.jar:lib/ojdbc5.jar:lib/log4j-1.2.17.jar:/ORACLE_HOME/server/lib/wlfullclient.jar:/ORACLE _HOME/server/lib/weblogic.jar:Alerts.jar... (9 Replies)
Discussion started by: ajothi
9 Replies

4. Shell Programming and Scripting

Use “tail -f” to monitor and report, but the top line should be always fixed on the screen.

Title: Use “tail -f” to monitor and report, but the top line should be always fixed on the screen. Hi, dear Unix experts, I am trying to find a Unix command (or scripting) on how to continuously display a text file of its last several lines of contents. But during this displaying, I want some... (2 Replies)
Discussion started by: df3c
2 Replies

5. Shell Programming and Scripting

Script to Monitor windows process

Hi Guys, I'm looking for a script( preferably perl script) which should monitor a application service and alert me when it goes down. application is installed in windows NT. Scenario: I have remedy ARS 6.1 installed in windows platform and need some script to monitor its service and alert me... (2 Replies)
Discussion started by: user__user3110
2 Replies

6. Shell Programming and Scripting

Script to monitor a process and track status in a file

i have a scenario where i need a script that monitors a process "Monitor" based on process id... there can be any number of instances of this running... i start this across 4 servers in NFS. Now i need a file which has the process ids of the process that are currently in execution at any... (9 Replies)
Discussion started by: niteesh_!7
9 Replies

7. Shell Programming and Scripting

script to monitor the process system when a process from user takes longer than 15 min run.

get email notification from from system when a process from XXXX user takes longer than 15 min run.Let me know the time estimation for the same. hi ,any one please tell me , how to write a script to get email notification from system when a process from as mentioned above a xxxx user takes... (1 Reply)
Discussion started by: kirankrishna3
1 Replies

8. Shell Programming and Scripting

Shell script to display top 5 process (per cpu consumption)

Hello, I have to create shell script being myself real novice in scripting... I was looking on the Internet for some script examples, but I don't have the basic understanding of the shell. I was trying to do something with “ps” command without success. For exemple total number of... (2 Replies)
Discussion started by: mr_awd
2 Replies

9. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

10. UNIX for Dummies Questions & Answers

Looking for monitor/top

I'm new to SUN (SunOS 5.8) and I can't find any monitoring tools to check cpu, memory, disk ... performance. Previously on unix servers I have used 'top' & 'monitor' but these commands are not on the box on in the man pages. Anyone suggest equivalent commands? (1 Reply)
Discussion started by: pavelmac
1 Replies
Login or Register to Ask a Question