I wrote small script under Debian Linux 11 that should check how many instances of application is currently running and what is power usage of GPU cards.
I save it under name test , and she is started every time I access instance over SSH
#!/bin/sh
clear
a=$(nvidia-smi -q -i 0 | grep "Power Draw" | cut -c45-50)
b=$(nvidia-smi -q -i 1 | grep "Power Draw" | cut -c45-50)
c=$(nvidia-smi -q -i 2 | grep "Power Draw" | cut -c45-50)
d=$(nvidia-smi -q -i 3 | grep "Power Draw" | cut -c45-50)
zet=$( echo "$a" + "$b" + "$c" + "$d" | bc -l )
echo "SYSTEM DRAW:" "$zet"
if [ "${zet}" -gt 150 ]; then
echo WARRNING - SYSTEM DRAW LOW
else
echo OK
fi
sleep 8
exit
5
Answers
All I need to add is this line x=${x%.*} That convert decimal number in number without decimals and script works perfect.
You could add
set -x
right before the part you want to debug, which will show you a debug of what is happening in bash. and stop it by inserting after thatset +x
like:
I do not think you are setting the value of
$150
The script might be failing if one of the compared values is not set.. so you should initialize your variables to be let’ say equal to
0
as a default at the beginning of your script, or via bashlike:
So many possible issues but you can compare possibly decimal values using
bc
:As the comments recommend, use shellcheck, however, I think your intention is not what you wrote.
Try this, create a script (i.e.
myscripy
)then run it
and prints
which is probably very different than what you expect.
That is because
power
will take the first argument’s value ($1
) and append50
.If you wanted argument number
150
(also very unlikely), you should writebut if you want just the number
edit
based on the comment, use
to calculate
zet
if the values are floating points.For the comparison use
One problem is that
[
(and[[
) do string comparisons, and you want a numeric comparison. For that you want to use((
, so something likewill work better. Otherwise a total of
1000
would print ok, and90
would give a warning.Other problems:
$150
gives you the value of a variable called150
— you probably want to remove the$
[[
and((
, a>
will do an output redirection, rather than being a ‘normal’ argument to a command such as[
.