Skip to content

Running a script right before shut-down of primary #3003

@ErikDB87

Description

@ErikDB87

Let me start by apologizing for submitting an "issue" for what is actually a question... But I read the documentation, and tried getting help via the mailing list.

I would like to gracefully shut down my router (Unifi Dream Router (UDR)) after my secondary has shut off (so my primary can follow up on the shut-down of the secondary), and before my primary shuts off (or in any case, before the UPS powers down). Unfortunately, the router doesn't seem to support NUT, but I've modified a bash script I found on the Ubiquiti forum, with which I can shut it down as user nut, running command sudo -H -u nut bash -c 'bash /var/lib/nut/NUT-scripts/shutdownrouter.sh'). (I assume that if any script is to be run, it would be run as user nut.)

erik@MinipcLG2:/var/lib/nut/NUT-scripts$ cat shutdownrouter.sh
#!/bin/bash

# Zie https://community.ui.com/questions/How-to-login-into-UniFi-OS-Console-using-shared-SSH-key-from-a-Mac/c177ccca-d67b-43a3-93fa-5c968ade0b81#answer/f5b9106c-0f3f-43a9-b26a-9bb8486e1ed1
# en https://community.ui.com/questions/Consoles-Monitoring-UPS-via-SNMP-for-Graceful-Shutdown/96979c34-7388-48f1-8053-a351c99c1b32#answer/159ab98f-0086-4215-a494-afa2b48a63bb

# Variables
ssh_timeout=30
ping_timeout=5

# Process for shutting down the UniFi-Controller
shutdown_controller() {
        echo "Shutting down the UniFi-Controller..."

        # Establish SSH-Connection using ssh key to the UniFi-Controller and send shutdown-command
        ssh -o ConnectTimeout=$ssh_timeout -i /var/lib/nut/.ssh/id_ed25519 [email protected] "ubnt-systool poweroff"

        # Verify, that the Controller did shut down
        ping -c 20 -W $ping_timeout 192.168.1.1

        if [ $? -eq 0 ]; then
                echo "SUCCESS: The UniFi-Controller has shut down properly."
                exit 0
                else
                echo "ERROR: The UniFi-Controller couldn't be shut down."
                exit 1
        fi
}

# Mainroutine
shutdown_controller

But now I'm trying to figure out how to call it, and I'm not sure... I assume in upsmon.conf there should be a line NOTIFYFLAG FSD SYSLOG+WALL+EXEC. But then I wondered whether FSD was maybe too late a flag?
Then I assume upssched.conf plays a role. This is the current setup:

erik@MinipcLG2:/etc/nut$ sudo cat upsmon.conf
RUN_AS_USER nut
MONITOR apcupskelder@localhost 1 upsmon (password) primary


MINSUPPLIES 1
SHUTDOWNCMD "/sbin/shutdown -h +0"
NOTIFYCMD /usr/sbin/upssched
POLLFREQ 2
POLLFREQALERT 1
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /run/nut/killpower
DEBUG_MIN 6

NOTIFYMSG ONLINE    "upsmon-logger: UPS %s on line power"
NOTIFYMSG ONBATT    "upsmon-logger: UPS %s on battery"
NOTIFYMSG LOWBATT   "upsmon-logger: UPS %s battery is low"
NOTIFYMSG FSD       "upsmon-logger: UPS %s: forced shutdown in progress"
NOTIFYMSG COMMOK    "upsmon-logger: Communications with UPS %s established"
NOTIFYMSG COMMBAD   "upsmon-logger: Communications with UPS %s lost"
NOTIFYMSG SHUTDOWN  "upsmon-logger: Auto logout and shutdown proceeding"
NOTIFYMSG REPLBATT  "upsmon-logger: UPS %s battery needs to be replaced"
NOTIFYMSG NOCOMM    "upsmon-logger: UPS %s is unavailable"
NOTIFYMSG NOPARENT  "upsmon-logger: upsmon parent process died - shutdown impossible"

NOTIFYFLAG ONLINE   SYSLOG+WALL+EXEC
NOTIFYFLAG ONBATT   SYSLOG+WALL+EXEC
NOTIFYFLAG LOWBATT  SYSLOG+WALL
NOTIFYFLAG FSD      SYSLOG+WALL+EXEC
NOTIFYFLAG COMMOK   SYSLOG+WALL+EXEC
NOTIFYFLAG COMMBAD  SYSLOG+WALL+EXEC
NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC
NOTIFYFLAG REPLBATT SYSLOG+WALL
NOTIFYFLAG NOCOMM   SYSLOG+WALL+EXEC
NOTIFYFLAG NOPARENT SYSLOG+WALL

RBWARNTIME 43200

NOCOMMWARNTIME 600

FINALDELAY 30
erik@MinipcLG2:/etc/nut$ sudo cat upssched.conf
CMDSCRIPT /etc/nut/upssched-cmd.sh
PIPEFN /etc/nut/upssched.pipe
LOCKFN /etc/nut/upssched.lock

# Starts a timer when the UPS switches to battery power
AT ONBATT * START-TIMER shutdown_timer 1200

# Cancels the shutdown timer when power is restored
AT ONLINE * CANCEL-TIMER shutdown_timer

# Executes immediate shutdown when battery is low
AT LOWBATT * EXECUTE immediate_shutdown

# Starts a timer on communication failure
AT COMMBAD * START-TIMER commbad_timer 1200

# Cancels the communication failure timer when communication is restored
AT COMMOK * CANCEL-TIMER commbad_timer

# Executes shutdown on persistent communication failure
AT NOCOMM * EXECUTE commbad_shutdown

# Executes powerdown on system shutdown
AT SHUTDOWN * EXECUTE powerdown

Then I assume upssched-cmd.sh plays a role. This is the current setup:

erik@MinipcLG2:/etc/nut$ sudo cat upssched-cmd.sh
#!/bin/sh

case $1 in
    shutdown_timer)
        # Log the event and initiate a controlled shutdown
        logger -t upssched-cmd "UPS running on battery for too long, initiating shutdown"
        /usr/sbin/upsmon -c fsd
        ;;

    immediate_shutdown)
        # Log the critical battery status and initiate immediate shutdown
        logger -t upssched-cmd "UPS on battery critical, forced shutdown"
        /usr/sbin/upsmon -c fsd
        ;;

    commbad_timer)
        # Log persistent communication failures and initiate shutdown
        logger -t upssched-cmd "UPS communication failure persists, initiating shutdown"
        /usr/sbin/upsmon -c fsd
        ;;

    commbad_shutdown)
        # Log communication failure and initiate shutdown
        logger -t upssched-cmd "UPS communication failed, initiating shutdown"
        /usr/sbin/upsmon -c fsd
        ;;

    powerdown)
        # Log the execution of the shutdown
        logger -t upssched-cmd "Executing powerdown command"
        ;;

    *)
        # Log unknown commands
        logger -t upssched-cmd "Unrecognized command: $1"
        ;;
esac

Where should I squeeze in bash /var/lib/nut/NUT-scripts/shutdownrouter.sh?

Or should/could I change SHUTDOWNCMD in upsmon.conf to bash /var/lib/nut/NUT-scripts/shutdownrouter.sh and end that script with /sbin/shutdown -h +0?

I know I can toy around with it, but I prefer asking experts for help, rather than maybe creating a situation which is not ideal.

Thanks for anyone's help!

Kind regards,

Erik

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions