User Tools

Site Tools


software:nut:stagedshutdown

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.

Set up actions in upsmon.conf

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.

Values for NOTIFYFLAG

NOTIFYFLAG can have an action of one or more of

  • SYSLOG - put a message in syslog
  • WALL - Use wall to send a message to all logged in users
  • EXEC - Run the script defined in NOTIFYCMD
  • IGNORE - don't do anything. Do not put any other flags on this line.

Note that NOTIFYCMD must be able to handle the flag in question if EXEC is set. Multiple actions can be performed on one flag by separating them with the plus sign, ie SYSLOG+WALL+EXEC will put a message in syslog, send out a wall message, and run the NOTIFYCMD script

Flags can be one of the following. By default, they are set up for SYSLOG+WALL

  • ONLINE - UPS is (back) online
  • ONBATT - UPS is on battery
  • LOWBATT - UPS is on battery and the battery has gone under a UPS specific value (10%)
  • FSD - NUT has started a Forced Shut Down
  • COMMOK - Communication has been reestablished
  • COMMBAD - Communication with the UPS has been lost
  • SHUTDOWN - UPS is in a shutdown state
  • REPLBATT - UPS thinks the batteries need to be replaced
  • NOCOMM - No communication with the UPS
  • NOPARENT - This script has no parent. Generally happens during a shutdown
  • CAL - The UPS is in calibration mode
  • NOTCAL - The UPS has returned from calibration mode
  • OFF - The UPS I is off
  • NOTOFF - The UPS is back on
  • BYPASS - The UPS is in bypass mode
  • NOTBYPASS - The UPS has returned from bypass mode

Create script to run on event

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@192.168.1.100 battery.runtime
# how much time has it been on already
upsc ups1@192.168.1.100 battery.runtime.elapsed
software/nut/stagedshutdown.txt · Last modified: 2025/04/14 23:24 by rodolico