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

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?

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

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
Random Solutions  
 
programming4us programming4us