2

I am setting up a UPS on ubuntu for the first time. I have exactly followed the instructions here: https://www.sobyte.net/post/2023-03/ups-nut-configuration-tutorial/

My UPS is a PowerShield Defender 650.
The correct driver for this is "blazer_usb".
I've named the device pshield and commands such as

upsc pshield

work fine.

Syslog also shows that the system does recognise when battery power is activated. Eg:

01/12/2024 9:43:20 PM
Jan 12 21:43:19 barbelith upsmon[2624]: UPS pshield@localhost on battery
01/12/2024 9:43:20 PM
Jan 12 21:43:19 barbelith upssched[154701]: Timer daemon started
01/12/2024 9:43:20 PM
Jan 12 21:43:19 barbelith upssched[154701]: New timer: onbattwarn (10 seconds)
01/12/2024 9:43:20 PM
Jan 12 21:43:19 barbelith upssched[154700]: Executing command: onbattnoti
01/12/2024 9:43:20 PM
Jan 12 21:43:19 barbelith upssched[154700]: exec_cmd(/opt/scripts/upssched-cmd.sh onbattnoti) returned 1
01/12/2024 9:43:30 PM
Jan 12 21:43:29 barbelith upssched[154701]: Event: onbattwarn 
01/12/2024 9:43:30 PM
Jan 12 21:43:29 barbelith upssched[154701]: exec_cmd(/opt/scripts/upssched-cmd.sh onbattwarn) returned 1
01/12/2024 9:43:45 PM
Jan 12 21:43:44 barbelith upssched[154701]: Timer queue empty, exiting

But the actual script (/opt/scripts/upssched-cmd.sh) does not seem to be running. I.e., the system is not shutting down. How can I fix this?

Relevant files:

/opt/scripts/upssched-cmd.sh

#!/usr/bin/env bash
# from https://www.sobyte.net/post/2023-03/ups-nut-configuration-tutorial/
set -ex

exec >> /var/log/upssched-cmd.log 2>&1

# Functions mainly responsible for shutting down the system
function xpoweroff(){
    logger -t upssched-cmd 'Preparing to shut down barbelith...'
    logger -t upssched-cmd 'All necessary systems have been successfully shut down....'
}

# Determine the event triggered by upssched
case $1 in
    onbattwarn)
        logger -t upssched-cmd 'The UPS has switched to battery power, ready to safely shut down the system...'
        xpoweroff
        ;;
    ups-back-on-line)
        logger -t upssched-cmd 'Municipal power has been restored...'
        ;;
    lowbatt)
        logger -t upssched-cmd 'UPS power is low, shut down the system immediately...'
        xpoweroff
        ;;
    *)
        logger -t upssched-cmd "Unrecognized command: $1"
        ;;
esac

/etc/nut/ups.conf

[pshield]
        driver = "blazer_usb"
        port = "auto"
        vendorid = "0665"
        productid = "5161"
        bus = "001"

/etc/nut/upsd.conf

LISTEN 0.0.0.0 3493

/etc/nut/upsmon.conf

SHUTDOWNCMD "/usr/sbin/poweroff"
MONITOR pshield@localhost 1 monuser PASSWORD master
NOTIFYCMD /usr/sbin/upssched
NOTIFYFLAG ONLINE SYSLOG+EXEC+WALL
NOTIFYFLAG ONBATT SYSLOG+EXEC+WALL
NOTIFYFLAG LOWBATT SYSLOG+EXEC

/etc/nut/upssched.conf

CMDSCRIPT /opt/scripts/upssched-cmd.sh
PIPEFN /run/nut/upssched.pipe
LOCKFN /run/nut/upssched.lock
AT ONBATT * START-TIMER onbattwarn 30
AT ONLINE * CANCEL-TIMER onbattwarn
AT ONBATT * EXECUTE onbattnoti
3
  • I believe the relevant log is this line from upssched: exec_cmd(/opt/scripts/upssched-cmd.sh onbattnoti) returned 1 so it may be useful to see the output when you try to run that command manually. Commented Jan 12 at 19:01
  • It looks like there were permission issues with that script and also with /var/log/upssched-cmd.log I've resolved these and the script does now seem to run at the correct time. Now I get the 'Preparing to shutdown..." line in the log, but the system does not actually shut down. I'm beginning to think that guide I followed (linked above) is too complicated for what I need. I just want the system to shutdown 3 minutes after the UPS switches to battery. Is there a simpler/better way to do this?
    – mob
    Commented Jan 14 at 2:50
  • 1
    There is no mechanism in the script you posted to shut anything down, I was not sure if that was handled by one of the other services you mentioned. Commented Jan 15 at 21:14

1 Answer 1

1

I've just solved exactly the same issue, which occurred on a Debian Linux 11-based system.

The main problem was that the nut user had no permission to run the shutdown command.

So, as sudo is installed on the system, I've just created a custom permission for this purpose:

sudo visudo /etc/sudoers.d/nut

File content:

nut ALL = (root) NOPASSWD: /sbin/shutdown

Just adapt the content to your needs.  Note that full path is required for the command (you may use the which command to find it out).

By the way: always use visudo to edit your sudoers files (i.e., avoid the temptation to use just plain vi, nano, or any other ordinary editor), as a small mistake may break the sudo command itself (which is required to modify these files!).

visudo at least performs a simple syntax check and won't let you save a corrupt file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .