From 8bd7314490e12d5501818e597144ffd5acc75564 Mon Sep 17 00:00:00 2001 From: u Date: Mon, 1 May 2023 10:55:01 -0400 Subject: separate service, executable, and installer --- Makefile | 22 +++++++ go.sh | 104 --------------------------------- suspend-below-battery-capacity | 51 ++++++++++++++++ suspend-below-battery-capacity.service | 9 +++ 4 files changed, 82 insertions(+), 104 deletions(-) create mode 100644 Makefile delete mode 100755 go.sh create mode 100755 suspend-below-battery-capacity create mode 100644 suspend-below-battery-capacity.service diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c876ade --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +UNIT = suspend-below-battery-capacity +BINDIR = /usr/local/bin +BINARIES = $(UNIT) +.PHONY: all install-bin install + +all: + @echo "run 'make install' to install" + +ifeq ($(shell id -u),0) + +install: $(UNIT).service install-bin + systemctl enable --now $(shell realpath -e $<) + +install-bin: + install -t $(BINDIR) -- $(BINARIES) + +else + +install install-bin: + sudo $(MAKE) -$(MAKEFLAGS) $@ + +endif diff --git a/go.sh b/go.sh deleted file mode 100755 index b9d6aea..0000000 --- a/go.sh +++ /dev/null @@ -1,104 +0,0 @@ -#!/bin/bash -default_capacity_threshold=50 -capacity_threshold_file=$HOME/.config/powerloss-handler/capacity_threshold - -battery_dir=/sys/class/power_supply/BAT0 -status_file=$battery_dir/status -capacity_file=$battery_dir/capacity - -UNIT=powerloss-handler -CMD=$0 - -FORCE_SUSPEND= - -self_install() -{ - if systemctl --user is-active "$UNIT" >/dev/null - then - systemctl --user stop "$UNIT" - fi - systemctl --user reset-failed "$UNIT" 2>/dev/null - systemd-run --user \ - --unit "$UNIT" \ - --property Restart="on-failure" \ - -- "$CMD" -} - -self_test() -{ - FORCE_SUSPEND=y - go - echo -} - -do_suspend() -{ - systemctl suspend -} - -go() -{ - read capacity < "$capacity_file" || return - case "$capacity" in - '' | *[^0-9]*) - echo "Error: unexpected value for battery capacity: $capacity" >&2 - return 1 ;; - esac - read status < "$status_file" || return - - if [ "$FORCE_SUSPEND" ] - then - status=Discharging - capacity=0 - echo 'Warning: test: pretending battery is discharging at 0% capacity' >&2 - fi - - if [ -t 2 ] - then - printf '\r %20s %.2d%%\r' "$status" "$capacity" >&2 - fi - - if [ -f "$capacity_threshold_file" ] - then - read capacity_threshold < "$capacity_threshold_file" - else - capacity_threshold=$default_capacity_threshold - fi - - [ "$status" = Discharging ] || return 0 - if [ "$capacity" -lt "$capacity_threshold" ] - then - do_suspend - fi -} - -main() -{ - if [ -e /usr/lib/bash/sleep ] - then - enable -f /usr/lib/bash/sleep sleep - else - echo 'Warning: not found: /usr/lib/bash/sleep (try: "sudo apt install bash-builtins")' >&2 - fi - - while true - do - go - sleep 1 - done -} - -if [ $# = 0 ] -then - main -else - case "$1" in - run) main ;; - install) self_install ;; - test) self_test ;; - *) echo "Error: unknown command: $*" >&2 - echo "Valid commands: run install test" >&2 - exit 1 - ;; - esac -fi diff --git a/suspend-below-battery-capacity b/suspend-below-battery-capacity new file mode 100755 index 0000000..114cf9b --- /dev/null +++ b/suspend-below-battery-capacity @@ -0,0 +1,51 @@ +#!/bin/bash +default_capacity_threshold=50 + +if [ -e /usr/lib/bash/sleep ] +then + enable -f /usr/lib/bash/sleep sleep +else + echo 'Warning: not found: /usr/lib/bash/sleep (try: "sudo apt install bash-builtins")' >&2 +fi + +main() +{ + capacity_threshold=${1:-$default_capacity_threshold} + while true + do + check_batteries /sys/class/power_supply/BAT* + sleep 1 + done +} + +check_batteries() +{ + for dir in "$@" + do + [ -d "$dir" ] || continue + check_battery "$dir" + done +} + +check_battery() +{ + battery_dir=$1 + + read status < "$battery_dir"/status || return + [ "$status" = Discharging ] || return 0 + + read capacity < "$battery_dir"/capacity || return + case "$capacity" in + '' | *[^0-9]*) + echo "Error: unexpected value for battery capacity: $capacity" >&2 + return 1 + ;; + esac + + if [ "$capacity" -lt "$capacity_threshold" ] + then + systemctl suspend + fi +} + +main "$@" diff --git a/suspend-below-battery-capacity.service b/suspend-below-battery-capacity.service new file mode 100644 index 0000000..bd42596 --- /dev/null +++ b/suspend-below-battery-capacity.service @@ -0,0 +1,9 @@ +[Unit] +Description = Suspend machine when battery capacity drops below threshhold + +[Service] +ExecStart = suspend-below-battery-capacity 50 +Restart = on-failure + +[Install] +WantedBy = default.target -- cgit v1.2.3