Solved

Unix Script for getting PID of process if Kernel exceeds 80%

Posted on 2009-04-10
4
521 Views
Last Modified: 2013-12-26
I want to write a Unix Shell script which should write the PID of the process with the CPU% it is using into a file when the Kernel utilization increases beyond 80%. How to do that?
0
Comment
Question by:srikanthradix
  • 2
  • 2
4 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24118684
Hi,
let's see if I got you right -
You want to examine your system's total cpu utilization and write out the top cpu consuming process along with its PID to a file? The problem I have here: I don't know what you mean with "Kernel utilization" - but
- assuming that you mean "User + System CPU", you could use the following:

#!/bin/ksh
hwm=80numtop=1log=/tmp/tops.log
if [ $(vmstat 1 1 | tail -1 | awk "{print $14 + $15}") -gt $hwm ]
 then
  date >> $log
  ps -ef -o comm -o pid -o pcpu | sort -k3nr | head -$numtop >> $log
  echo "----------------------------" >> $log
 fi
exit

You can adjust the high water mark (hwm=80) and the number of top processes to log (numtop=1)
Configure your logfile by tailoring log=......
I added the command name (-o comm) of the process to the output (along with PID and percent CPU, as you desired).
If you don't need it, remove '-o comm' from the "ps -ef ...' line.

wmp

0
 

Author Comment

by:srikanthradix
ID: 24120111
How to make this script run forever as a daemon? I want to monitor the cpu usage for 2 days.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 24120217
Two possibilities -

1) Create a cron job to run let's say every 15 minutes or so -
00,15,30,45 * * * * /path/to/the/script

2) Run it in background, have it wake up every 15 minutes -

#!/bin/ksh
interval=900
hwm=80
numtop=1
log=/tmp/tops.log
while true do
   if [ $(vmstat 1 1 | tail -1 | awk "{print $14 + $15}") -gt $hwm ]
    then
     date >> $log
     ps -ef -o comm -o pid -o pcpu | sort -k3nr | head -$numtop >> $log
     echo "----------------------------" >> $log
   fi
  sleep $interval done
exit

Configure the desired interval (in seconds) by modifying interval=...
and start the script with

nohup /path/to/the/script &

Please take care to watch the size of your logfile, as it will of course grow over time.

wmp
0
 

Author Closing Comment

by:srikanthradix
ID: 31568987
That is amazing. I have learnt something today. Thanks for answer.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now