Tuesday, February 13, 2007

Monitor CPU Usage for Each Process

When I'm looking for CPU hogs (processes), I like to use an alternate ps command. This command shows the percentage of CPU usage in ascending order.

# /usr/ucb/ps -aux | head -20
USER PID %CPU %MEM SZ RSS TT S START TIME COMMAND
root 2412 1.5 2.1 2840 2512 pts/2 O 23:25:35 0:00 /usr/ucb/ps -aux
root 2411 0.1 0.6 952 664 pts/2 S 23:25:35 0:00 head -20
root 353 0.0 1.6 2384 1952 ? S Nov 06 3:31 mibiisa -r -p 3280
root 0 0.0 0.0 0 0 ? T Nov 06 0:17 sched
root 1 0.0 0.3 792 376 ? S Nov 06 0:00 /etc/init -
root 2 0.0 0.0 0 0 ? S Nov 06 0:00 pageout
root 3 0.0 0.0 0 0 ? S Nov 06 1:45 fsflush
root 56 0.0 1.0 1544 1144 ? S Nov 06 0:00 /usr/lib/sysevent/

# while true
do
/usr/ucb/ps -aux | head -20
sleep 5
done

In terms of CPU percentage, the aforementioned loop shows the top 20 processes every 5 seconds.

You might want look at prstat, too, to check out the active stats

# prstat
PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP
2413 root 1536K 1280K cpu0 58 0 0:00.00 0.1% prstat/1
207 root 3688K 3112K sleep 58 0 0:00.00 0.0% nscd/8
353 root 2384K 1952K sleep 48 0 0:00.00 0.0% mibiisa/12
240 root 3240K 1608K sleep 59 0 0:00.00 0.0% sendmail/1
228 root 1408K 944K sleep 53 0 0:00.00 0.0% powerd/5
326 root 3088K 2016K sleep 22 0 0:00.00 0.0% dmispd/5
193 root 2000K 1376K sleep 33 0 0:00.00 0.0% cron/1
187 root 3472K 2016K sleep 52 0 0:00.00 0.0% syslogd/9
167 root 1984K 1416K sleep 12 0 0:00.00 0.0% lockd/1
215 root 3072K 976K sleep 43 0 0:00.00 0.0% lpsched/1

2 comments:

bob said...

You can also use the top command

Anonymous said...

Bob - he's talking Unix and not Linux so you'd have to add top to use it.. :-\ (which I did by the way:)