1

I have attached my UPS to ubuntu and it's recognised. However I don't like the options that I see in ubuntu power settings. I have another PC also attached to same UPS. So I want ubuntu desktop to execute script when power is about 50% so my second pc can also properly hibernate. And then I can hibernate my ubuntu PC with same script.

Is it possible to do that in the way that is not breaking ubuntu implpementation of UPS monitor? I have read that I can install nut but I have some errors, maybe it's a conflict

● nut-monitor.service - Network UPS Tools - power device monitor and shutdown controller
   Loaded: loaded (/lib/systemd/system/nut-monitor.service; enabled; vendor preset: enabled)
   Active: failed (Result: protocol) since Sat 2019-07-13 02:09:46 MSK; 11ms ago
  Process: 13906 ExecStart=/sbin/upsmon (code=exited, status=0/SUCCESS)

Maybe I can catch some dmesg message or check the status of UPS with some native tool?

1
  • The command apt-cache search nut|grep -w nut will show several packages, including nut-doc - network UPS tools - documentation and python-nut - network UPS tools - Python bindings for NUT server. Also, are you sure nut-monitor is happy? The Active: failed part bothers me.
    – waltinator
    Jul 13, 2019 at 1:01

1 Answer 1

1

Ubuntu automatically keeps track of all batteries status:

Ubuntu Batteries Statuses

The same information displayed in the GUI screen above can also be accessed from your terminal / shell / bash script (they are all sort of the same thing in many ways).

To get the same information from CLI use:

$ upower -i $(upower -e | grep -i UPS)
  native-path:          /sys/devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1.2/1-1.2:1.0/usbmisc/hiddev2
  vendor:               CPS
  model:                CP550HGa
  serial:               BFBB104#BI1.g
  power supply:         yes
  updated:              Fri 12 Jul 2019 06:35:56 PM MDT (12 seconds ago)
  has history:          yes
  has statistics:       yes
  ups
    present:             yes
    state:               fully-charged
    warning-level:       none
    time to empty:       25.5 minutes
    percentage:          100%
    icon-name:          'battery-full-charged-symbolic'

Then to narrow it down to percentage use:

$ upower -i $(upower -e | grep -i UPS) | grep -i percentage
    percentage:          100%

Then to extract the second column use:

$ upower -i $(upower -e | grep -i UPS) | grep -i percentage | cut -d':' -f2
          100%

Then to extract only digits and eliminate leading spaces and trailing % use:

$ upower -i $(upower -e | grep -i UPS) | grep -i percentage | sed 's/[^0-9]*//g'
100

Now assign what you want to a variable and display it:

$ PERCENT=$(upower -i $(upower -e | grep -i UPS) | grep -i percentage | sed 's/[^0-9]*//g')
$ echo $PERCENT
100

Next step is to write a script something like this

#!/bin/bash

while true; do
    PERCENT=$(upower -i $(upower -e | grep -i UPS) | grep -i percentage \
        | sed 's/[^0-9]*//g')
    if [[ "$PERCENT" -lt 50 ]] ; then
        # email my cell phone
        mail -s "Electricity grid has shut down, run home" [email protected]
        # text my cell phone
        curl -X POST https://textbelt.com/text --data-urlencode \
            phone="999-333-4567" --data-urlencode \
            message="Electricity grid has shot down, run home" -d key=textbelt
    fi
    sleep 300 # Sleep for 5 minutes to reduce resource usage
done

This is a script I would use, in your case adapt it to hibernate (I have a laptop so I never hibernate). My UPS is for window fan, not laptop which has it's own battery and is suspended when I'm at work. Technology can be used differently by different people.

1
  • That's more than enough, thanks! So I guess while true or cron script is the only option to check that, there is no "event" or "broadcast" that I can catch? Anyway cron every minute should do the job.
    – POMATu
    Jul 13, 2019 at 11:55

You must log in to answer this question.

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