[Nut-upsuser] NUT & UPS, how to shut down client after 2 min on battery?

Rafael Reinoso reinoso at hotmail.com
Thu Dec 22 12:47:01 GMT 2022


Hi!


Some systems I have take some time to shut down, so I would like them to shut down if the UPS have been on battery for 2 minutes.


Internet has told me that SHUTDOWNCMD “/sbin/shutdown -h +0” in /etc/nut/upsmon.conf will execute when the UPS is both OB (on battery) and LB (Low battery). Like in wiki.ipfire.org - Detailed NUT Configuration<https://wiki.ipfire.org/addons/nut/detailed> where it says

“SHUTDOWNCMD “/sbin/shutdown -h +0” this command will be triggered if UPS sends the BL (Battery Low) flag.”

But I would like to be on the safest side, and not hope “low battery” is enough time to shut down. So 2 minutes sound much more safe for paranoid me.


I do not know much about NUT, but after some Googling, one idea I have is to in /etc/nut/upsmon.conf on client change to this

NOTIFYCMD "/etc/nut/shutdown_script.sh"
NOTIFYFLAG ONBATT EXEC


And then in /etc/nut/shutdown_script.sh have something like this

#!/bin/bash

poll_interval=5
wait_time=120
counter=0
no_wait_time=60
no_counter=0

while true
do
  output=$(upsc qnapups at 192.168.222.252 ups.status)
  if echo "$output" | grep -q "OB"
  then
    counter=$((counter + poll_interval))
    no_counter=0
  else
    counter=0
    no_counter=$((no_counter + poll_interval))
  fi
  if [ $counter -ge $wait_time ]
  then
    echo "Still outoput OB = On Battery, after 2 min. I will now shut down"
        # Here I will shutdown the system
        # "/sbin/shutdown -h +0"
    break
  fi
  if [ $no_counter -ge $no_wait_time ]
  then
    echo "Have not seen OB for 1 min, so quiting the script"
    break
  fi
  sleep $poll_interval
done


But now I am starting to wonder if I am overdoing this. Surely there must already be a ready-made solution for this in NUT somewhere, but I have just searched with wrong words i Google.


Have any of you done something similar with NUT, and what is the right way to do it?


/raffe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/nut-upsuser/attachments/20221222/8ee9f074/attachment-0001.htm>


More information about the Nut-upsuser mailing list