1833589 Members
3956 Online
110061 Solutions
New Discussion

A script about TOP

 
ericfjchen
Regular Advisor

A script about TOP

There is a process occupied over 99% CPU (running time > 240 min) as below,

PID USERNAME PRI NICE SIZE RES STATE TIME CPU COMMAND
91847 prodmgr 44 0 57M 18M sleep 240:02 99.00% f60webmx
-----------------
Because we have 10 CPU in the server, so we can't detect this process by total CPU loading.
Can you tell me how to write a sciprt to detect a process (CPU > 99%, Run Time > 240)?

Thanks

Eric
5 REPLIES 5
Robert-Jan Goossens
Honored Contributor

Re: A script about TOP

Hi Eric,

Try this one.

# echo " %CPU PID RUSER COMMAND" ;UNIX95= ps -ef -o 'pcpu pid ruser args'|sort -nr|head -10

top 10 cpu

# UNIX95= ps -e -o "user,pcpu,cpu,vsz,pid,ppid,args" | sort -rnk2 | head -10

Regards,
Robert-Jan

Ps.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=4177
spex
Honored Contributor

Re: A script about TOP

Hi Eric,

Give this quasi-tested script a shot (adjust THRESHOLD_CPU and THRESHOLD_RT accordingly):

#!/usr/bin/sh
typeset -i THRESHOLD_CPU=99
typeset -i THRESHOLD_RT=240
top -d 1 -n 30 -f /tmp/top.$$.1
awk -v T_CPU=${THRESHOLD_CPU} -v T_RT=${THRESHOLD_RT} 'NR>15{ sub(/:../,"",$10); if ($11 > T_CPU && $10 > T_RT) print}' < /tmp/top.$$.1 > /tmp/top.$$.2
if [[ -s /tmp/top.$$.2 ]]
then
mailx -m -s "Process(es) Hogging CPU!" your@email.addr < /tmp/top.$$.2
fi
rm -f /tmp/top.$$.[12]
exit 0

PCS
ericfjchen
Regular Advisor

Re: A script about TOP

typeset: not found, why? Pls help!
Arturo Galbiati
Esteemed Contributor

Re: A script about TOP

Hi Spex,
some note about your script:
you use NR>15 to skip the header line in TOP output till the first line after CPU but this is not fixed. It depends on teh number of CPU you have in my case is 27:
24
25 Memory: 11548788K (5845412K) real, 14852188K (8039792K) virtual, 16883816K free Page# 1/42
26
27 CPU TTY PID USERNAME PRI NI SIZE RES STATE TIME %WCPU %CPU COMMAND
28 11 ? 25893 oracle 149 22 1136M 51436K sleep 53:37 40.19 40.12 oracleGPWK01CS

in awk simply use:
/tmp/top.$$.1
instead of < /tmp/top.$$.1

HTH,
Art
lawrenzo
Trusted Contributor

Re: A script about TOP

something a little simpler:

ps aux |tail +2 |sort -rn -k 3,3 |more
hello