skip to Main Content

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


  1. Chosen as BEST ANSWER

    All I need to add is this line x=${x%.*} That convert decimal number in number without decimals and script works perfect.


  2. 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 that set +x

    like:

    set -x
    power=$150
    echo "SYSTEM DRAW :" $total
    if [ $total > $power ] ; then # escape > otherwise it redirects output
    

    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 bash

    like:

    power=${150:-10} # if $150 does not have a value or empty, the default value of $power will be set to `10`
    
    Login or Signup to reply.
  3. So many possible issues but you can compare possibly decimal values using bc:

    if [ "$(echo "$total > $power" | bc)" = 1 ]; then
    
    Login or Signup to reply.
  4. As the comments recommend, use shellcheck, however, I think your intention is not what you wrote.

    Try this, create a script (i.e. myscripy)

    #! /bin/bash
    
    power=$150
    echo "power=$power"
    

    then run it

    ./myscript abc
    

    and prints

    power=abc50
    

    which is probably very different than what you expect.

    That is because power will take the first argument’s value ($1) and append 50.

    If you wanted argument number 150 (also very unlikely), you should write

    power=${150}
    

    but if you want just the number

    power=150
    

    edit

    based on the comment, use

    zet=$(bc <<<"$a+$b+$c+$d")
    

    to calculate zet if the values are floating points.

    For the comparison use

    if [ "$(bc <<<"$zet>150")" == 1 ]; then
      ...
    fi
    
    Login or Signup to reply.
  5. One problem is that [ (and [[) do string comparisons, and you want a numeric comparison. For that you want to use ((, so something like

    if (( $total > 150 )); then
        echo "WARNING..."
    else
        echo "OK"
    fi
    

    will work better. Otherwise a total of 1000 would print ok, and 90 would give a warning.

    Other problems:

    • $150 gives you the value of a variable called 150 — you probably want to remove the $
    • Outside of special forms like [[ and ((, a > will do an output redirection, rather than being a ‘normal’ argument to a command such as [.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search