Solved

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

Posted on 2009-04-10
4
519 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That is amazing. I have learnt something today. Thanks for answer.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…

771 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