Shell Script to grep output from top command.

Hello,

I want a script which would grep details from top command for specific processes. I m not sure of the PID of these processes but i know the names.

[xmp@tswebpxmp8 ~]$ top -c
top - 16:41:55 up 160 days,  5:53,  2 users,  load average: 9.36, 9.18, 8.98
Tasks: 288 total,   9 running, 279 sleeping,   0 stopped,   0 zombie
Cpu(s): 34.0%us, 16.3%sy,  0.0%ni, 45.3%id,  0.3%wa,  0.2%hi,  3.9%si,  0.0%st
Mem:  49452208k total, 49154692k used,   297516k free,   539372k buffers
Swap: 16779884k total,      244k used, 16779640k free, 32430072k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5473 xmp       25   0 51224  34m 2864 R 99.7  0.1   1:53.26 logscan -in /var/xmp/logmgmt/work_billing/pending/XMP_tswebpxmp8_1203140830.log -out XMP_tswebpxmp8_1203140
23199 xmp       15   0 1135m 1.1g 5660 R 81.4  2.2   5398:45 cee -n TCP-ROUTER.tswebpxmp8.4
28549 xmp       16   0 1264m 1.2g 5660 R 73.8  2.5   7514:20 cee -n TCP-ROUTER.tswebpxmp8.11
32675 xmp       16   0 1274m 1.2g 5660 R 72.1  2.5   7297:01 cee -n TCP-ROUTER.tswebpxmp8.12
23148 xmp       15   0 1147m 1.1g 5660 S 71.8  2.3   5698:58 cee -n TCP-ROUTER.tswebpxmp8.2
26869 xmp       16   0 1147m 1.1g 5660 R 66.2  2.3   5644:15 cee -n TCP-ROUTER.tswebpxmp8.8
24956 xmp       15   0 1142m 1.1g 5660 R 59.2  2.2   5706:29 cee -n TCP-ROUTER.tswebpxmp8.6
21247 xmp       15   0 1205m 1.1g 5672 S 58.5  2.4   5316:25 cee -n TCP-ROUTER.tswebpxmp8.1
26862 xmp       15   0 1130m 1.0g 5660 S 57.5  2.2   5451:44 cee -n TCP-ROUTER.tswebpxmp8.7
23155 xmp       15   0 1117m 1.0g 5660 S 48.2  2.2   5261:25 cee -n TCP-ROUTER.tswebpxmp8.3
26870 xmp       15   0 1119m 1.0g 5660 S 47.9  2.2   5542:04 cee -n TCP-ROUTER.tswebpxmp8.9
24955 xmp       16   0 1163m 1.1g 5660 S 46.9  2.3   5607:23 cee -n TCP-ROUTER.tswebpxmp8.5
28546 xmp       15   0 1132m 1.0g 5660 R 46.9  2.2   5472:32 cee -n TCP-ROUTER.tswebpxmp8.10

I want to grep details of every TCP-ROUTER process. how do i do it.

-Siddhesh

 
top -b -n 1 | grep "TCP-ROUTER"

I tried this, it does not work

[xmp@tswebpxmp7 ~]$ top -b -n 1 | grep "TCP-ROUTER"
[xmp@tswebpxmp7 ~]$ top -b -n 1 | grep "TCP-ROUTER*"
[xmp@tswebpxmp7 ~]$ top -b -n 1 | grep TCP-ROUTER
[xmp@tswebpxmp7 ~]$ top -b -n 1 | grep TCP-ROUTER*

-Siddhesh

Is the below top command shows the "TCP-ROUTER" ?

 
top -b -n 1

if you want to grep the process details, the use

 
ps -ef | grep "TCP\-ROUTER"

I want to get the process details of these. like memory utilization/CUP utilization which we get in top command.

-Siddhesh

Is the below top command shows the "TCP-ROUTER" ?

 top -b -n 1

provide the -c flag as well, you need the full command line for TCP-ROUTER to appear in the output, thus

 top -bcn 1 | grep "TCP-ROUTER"

Thanks that worked for me.

#!/bin/bash

top -bcn 1 | grep "TCP-ROUTER"

exit 0

How do i execute this script with nohup to run every 15 mins.?

-Siddhesh

---------- Post updated at 05:23 PM ---------- Previous update was at 04:53 PM ----------

can someone please tell me how to run script in nohup every 15 mins?

-Siddhesh

You can add below entry into cron.

 0,15,30,45 * * * * /path/to/command