User Tools

Site Tools


software:nut:stagedshutdown

This is an old revision of the document!


Staged NUT client shutdowns

Clients can be configured to shut down at different times. As far as I can tell, there is no facility for the NUT server to tell clients to shut down at different times, but each client can be configured separately. Thus, your YouTube player can go down after a minute, your power hungry workstation when the battery is 50% gone, and your router just before the UPS goes into sleep mode.

How to configure a client

Edit upsmon.conf and add the following lines

upsmon.conf
# set up to shut down 2 minutes after we are on battery
# to save battery life
NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC
NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC
NOTIFYCMD "/usr/local/etc/nut/notifycmd"

Note: You must put the path in quotes.

Now, create a new file in /usr/local/etc/nut named notifycmd. The path doesn't matter, and neither does the name, it just must match the last line (NOTIFYCMD).

notifycmd
#! /usr/bin/env sh
#
# NUT NOTIFYCMD script
 
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
 
trap "exit 0" SIGTERM
 
if [ "$NOTIFYTYPE" = "ONLINE" ]
then
        echo $0: power restored | wall
        # Cause all instances of this script to exit.
        killall -s SIGTERM `basename $0`
fi
 
if [ "$NOTIFYTYPE" = "ONBATT" ]
then
        echo $0: 3 minutes till system powers down... | wall
        # Loop with one second interval to allow SIGTERM reception.
        let "n = 180"
        while [ $n -ne 0 ]
        do
                sleep 1
                let "n--"
        done
        echo $0: commencing shutdown | wall
        upsmon -c fsd
fi

Save this and make it executable

chmod 700 /usr/local/etc/nut/notifycmd

Now, when the UPS reports it has been on battery, the script will be called. It will wait 3 minutes, then shutdown the server (upsmon -c fsd)

Each time the NUT server is polled, a new copy of the script will be created, so you'll get a new message. If, during the 3 minutes, power comes back on, the first stanza (“$NOTIFYTYPE” = “ONLINE”) will be called, and it will kill all copies of this script, aborting shutdown.

Note that $NOTIFYTYPE can have several values. The following list is taken from the comments in upsmon.conf.

  • ONLINE : UPS is back online
  • ONBATT : UPS is on battery
  • LOWBATT : UPS has a low battery (if also on battery, it's “critical”)
  • FSD : UPS is being shutdown by the primary (FSD = “Forced Shutdown”)
  • COMMOK : Communications established with the UPS
  • COMMBAD : Communications lost to the UPS
  • SHUTDOWN : The system is being shutdown
  • REPLBATT : The UPS battery is bad and needs to be replaced
  • NOCOMM : A UPS is unavailable (can't be contacted for monitoring)
  • NOPARENT : The process that shuts down the system has died (shutdown impossible)

You can track any/all of them in your shutdown command. Also, note that you can use upsc to check battery level, if you want.

# how long can the UPS stay up
upsc ups1@10.111.111.33 battery.runtime
# how much time has it been on already
upsc ups1@10.111.111.33 battery.runtime.elapsed
software/nut/stagedshutdown.1744689912.txt.gz · Last modified: 2025/04/14 23:05 by rodolico