Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am having an issue with a sctipt i am creating for monitoring of CPU usage.
To retreive the usage for a certain PID i am using the following:
getCPU=`prstat -p $PID 1 1 | awk '{print $9}' | cut -d'%' -f1 | cut -d',' -f1`
the output from the command is the following:
CPU
0.0
0.09is there a way that i can select the 0.0 value(the second row) as this is the CPU percentage used.
or is there a better command i can use to isolate this value, as i need the value to be an integer so that i can use a compare statement.
All help is greatly appreciated.

Hi,
This resolved the isse of retreiving the cpu usage number,
however with the if statements i am trying to use i get the following issue,
my updated statement is the following:
CPU=`/usr/bin/prstat -p $PID 1 1 | /usr/bin/awk 'NR == 2 {print $9}' | /usr/bin/cut -d'%' -f1`1. this if statement is ignored and the script runs through regardsless of the if statement (ie with a value of cpu usage of 0.4):
if [ $CPU > 40 ]
2. this if statement gives the following error message and does not get into the if statement.
if [ $CPU -gt 40 ]
./monit.sh: [: 0.0: integer expression expected
all help is greatly appreciated on this issue,
regards,
miike

Disclaimer: I can't test this reply because my version of UNIX doesn't have prstat, so I don't even know what its output looks like.
Try this pipe:
prstat -p $PID 1 1 | awk '{print $9}' | cut -d'.' -f1 | cut -d',' -f1Edit: Spelling.

Hi,
The output is:
0.4
the problem is the comparison, i've tried -gt and >, however when i use -gt it comes up with the error, and when i use > it goes into the if statement regardless of it being smaller in the comparison,
is there any way to compare the output (0.4) to make sure it only goes into the if statement when greater than 40,
cheers

Your problem is that the bourne shell, sh, and the '88 version of korn shell does not do decimal type arithmetic. The bc command, arbitrary precision command, is one way of solving this problem.
Go to this link for a bc example using an if statement:
http://daresler.net/info/shell-scri...

you can do everything in awk, no need for bc
[code]
#!/bin/sh
/usr/bin/prstat -p 13633 1 1 | nawk '
NR == 2 {
sub("%", "",$9)
print $9
if ($9 <= 40) {
print "lower"
}
else if ( $9 > 40 ) {
print "higher"
}
}'
[/code]

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |