Hi all,
I need a script that will check the cpu usage of a certain process and then kill the process if the % cpu is greater than 25%.
I know i can get the pid of a process by doing pidof <processname>.
Once I have the pid number is there a command in bash to return the current % cpu?
honglus
2
Linux: ps -aux
Solaris:/usr/ucb/ps -aux
I got it using this
PIDIDOFLTSESSION=`pidof vmware-vmx`
PERCENTAGECPUOFLTSESSION=`top -p $PIDIDOFLTSESSION -n 1 -b | grep $PIDIDOFLTSESSION | awk '{print $9}'`
#echo $PIDIDOFLTSESSION
#echo $PERCENTAGECPUOFLTSESSION
INTPERCENTAGECPUOFLTSESSION=`echo $PERCENTAGECPUOFLTSESSION | bc | sed 's/[.].*//'`
if [ $INTPERCENTAGECPUOFLTSESSION -gt 5 ]
then echo "its greater than 5"
else echo "its less than 5"
fi