I'm writing a shell script to find out if any process is taking too much CPU utilization, then the script will send out a mail to the support team.
I have threshold limit as 25, and taking CPU Usage as:
cpuUsage=`ps -eo pcpu,pid,args | sort -k 1 -nr | head -1`
Iterating over it to find out cpuUsage
for count in $cpuUsage
do
CPUusageCount=$count
done
Then Checking CPUUsageCount
with threshold limit as like this:
if [ $CPUusageCount -gt $THRESHOLD_LIMIT ];
then
#Sending mail to Support group
fi
Here I'm facing an error message: Integer expression expected at if [ $CPUusageCount
. Can't we use -gt
to validate floating point numbers? Please help me how to achieve it?
You could decide to chop the fractional part off the number, and then use -ge
to compare:
if [ "${CPUusageCount%.*}" -ge $THRESHOLD_LIMIT ]
then
# Send email
fi
You can use command-substitution
to compare two floating-point number.
CPUusageCount=99.99
THRESHOLD_LIMIT=55.55
if [ `python -c "print $CPUusageCount>$THRESHOLD_LIMIT"` == 'True' ]; then
echo Sending mail to Support group
fi
您可以使用bc命令:
if [ $(echo "$CPUusageCount >= $THRESHOLD_LIMIT"|bc) -eq 1 ];
Floating point arithmetic is supported by ksh
#!/bin/ksh -x
CPUusageCount=99.99
THRESHOLD_LIMIT=55.55
if [ $CPUusageCount -gt $THRESHOLD_LIMIT ]; then
echo Sending mail to Support group
fi
gives
+ CPUusageCount=99.99
+ THRESHOLD_LIMIT=55.55
+ [ 99.99 -gt 55.55 ]
+ echo Sending mail to Support group
Sending mail to Support group
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.