diff options
author | u <u@billy> | 2023-05-01 10:55:01 -0400 |
---|---|---|
committer | u <u@billy> | 2023-05-01 10:55:01 -0400 |
commit | 8bd7314490e12d5501818e597144ffd5acc75564 (patch) | |
tree | ce8462c3b5b8a704d6e33055cbffdd241f40d21a | |
parent | a1d4d87f480e932960d037e6401db87b92fdf4f6 (diff) |
separate service, executable, and installer
-rw-r--r-- | Makefile | 22 | ||||
-rwxr-xr-x | go.sh | 104 | ||||
-rwxr-xr-x | suspend-below-battery-capacity | 51 | ||||
-rw-r--r-- | suspend-below-battery-capacity.service | 9 |
4 files changed, 82 insertions, 104 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c876ade --- /dev/null +++ b/Makefile | |||
@@ -0,0 +1,22 @@ | |||
1 | UNIT = suspend-below-battery-capacity | ||
2 | BINDIR = /usr/local/bin | ||
3 | BINARIES = $(UNIT) | ||
4 | .PHONY: all install-bin install | ||
5 | |||
6 | all: | ||
7 | @echo "run 'make install' to install" | ||
8 | |||
9 | ifeq ($(shell id -u),0) | ||
10 | |||
11 | install: $(UNIT).service install-bin | ||
12 | systemctl enable --now $(shell realpath -e $<) | ||
13 | |||
14 | install-bin: | ||
15 | install -t $(BINDIR) -- $(BINARIES) | ||
16 | |||
17 | else | ||
18 | |||
19 | install install-bin: | ||
20 | sudo $(MAKE) -$(MAKEFLAGS) $@ | ||
21 | |||
22 | endif | ||
@@ -1,104 +0,0 @@ | |||
1 | #!/bin/bash | ||
2 | default_capacity_threshold=50 | ||
3 | capacity_threshold_file=$HOME/.config/powerloss-handler/capacity_threshold | ||
4 | |||
5 | battery_dir=/sys/class/power_supply/BAT0 | ||
6 | status_file=$battery_dir/status | ||
7 | capacity_file=$battery_dir/capacity | ||
8 | |||
9 | UNIT=powerloss-handler | ||
10 | CMD=$0 | ||
11 | |||
12 | FORCE_SUSPEND= | ||
13 | |||
14 | self_install() | ||
15 | { | ||
16 | if systemctl --user is-active "$UNIT" >/dev/null | ||
17 | then | ||
18 | systemctl --user stop "$UNIT" | ||
19 | fi | ||
20 | systemctl --user reset-failed "$UNIT" 2>/dev/null | ||
21 | systemd-run --user \ | ||
22 | --unit "$UNIT" \ | ||
23 | --property Restart="on-failure" \ | ||
24 | -- "$CMD" | ||
25 | } | ||
26 | |||
27 | self_test() | ||
28 | { | ||
29 | FORCE_SUSPEND=y | ||
30 | go | ||
31 | echo | ||
32 | } | ||
33 | |||
34 | do_suspend() | ||
35 | { | ||
36 | systemctl suspend | ||
37 | } | ||
38 | |||
39 | go() | ||
40 | { | ||
41 | read capacity < "$capacity_file" || return | ||
42 | case "$capacity" in | ||
43 | '' | *[^0-9]*) | ||
44 | echo "Error: unexpected value for battery capacity: $capacity" >&2 | ||
45 | return 1 ;; | ||
46 | esac | ||
47 | read status < "$status_file" || return | ||
48 | |||
49 | if [ "$FORCE_SUSPEND" ] | ||
50 | then | ||
51 | status=Discharging | ||
52 | capacity=0 | ||
53 | echo 'Warning: test: pretending battery is discharging at 0% capacity' >&2 | ||
54 | fi | ||
55 | |||
56 | if [ -t 2 ] | ||
57 | then | ||
58 | printf '\r %20s %.2d%%\r' "$status" "$capacity" >&2 | ||
59 | fi | ||
60 | |||
61 | if [ -f "$capacity_threshold_file" ] | ||
62 | then | ||
63 | read capacity_threshold < "$capacity_threshold_file" | ||
64 | else | ||
65 | capacity_threshold=$default_capacity_threshold | ||
66 | fi | ||
67 | |||
68 | [ "$status" = Discharging ] || return 0 | ||
69 | if [ "$capacity" -lt "$capacity_threshold" ] | ||
70 | then | ||
71 | do_suspend | ||
72 | fi | ||
73 | } | ||
74 | |||
75 | main() | ||
76 | { | ||
77 | if [ -e /usr/lib/bash/sleep ] | ||
78 | then | ||
79 | enable -f /usr/lib/bash/sleep sleep | ||
80 | else | ||
81 | echo 'Warning: not found: /usr/lib/bash/sleep (try: "sudo apt install bash-builtins")' >&2 | ||
82 | fi | ||
83 | |||
84 | while true | ||
85 | do | ||
86 | go | ||
87 | sleep 1 | ||
88 | done | ||
89 | } | ||
90 | |||
91 | if [ $# = 0 ] | ||
92 | then | ||
93 | main | ||
94 | else | ||
95 | case "$1" in | ||
96 | run) main ;; | ||
97 | install) self_install ;; | ||
98 | test) self_test ;; | ||
99 | *) echo "Error: unknown command: $*" >&2 | ||
100 | echo "Valid commands: run install test" >&2 | ||
101 | exit 1 | ||
102 | ;; | ||
103 | esac | ||
104 | 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 @@ | |||
1 | #!/bin/bash | ||
2 | default_capacity_threshold=50 | ||
3 | |||
4 | if [ -e /usr/lib/bash/sleep ] | ||
5 | then | ||
6 | enable -f /usr/lib/bash/sleep sleep | ||
7 | else | ||
8 | echo 'Warning: not found: /usr/lib/bash/sleep (try: "sudo apt install bash-builtins")' >&2 | ||
9 | fi | ||
10 | |||
11 | main() | ||
12 | { | ||
13 | capacity_threshold=${1:-$default_capacity_threshold} | ||
14 | while true | ||
15 | do | ||
16 | check_batteries /sys/class/power_supply/BAT* | ||
17 | sleep 1 | ||
18 | done | ||
19 | } | ||
20 | |||
21 | check_batteries() | ||
22 | { | ||
23 | for dir in "$@" | ||
24 | do | ||
25 | [ -d "$dir" ] || continue | ||
26 | check_battery "$dir" | ||
27 | done | ||
28 | } | ||
29 | |||
30 | check_battery() | ||
31 | { | ||
32 | battery_dir=$1 | ||
33 | |||
34 | read status < "$battery_dir"/status || return | ||
35 | [ "$status" = Discharging ] || return 0 | ||
36 | |||
37 | read capacity < "$battery_dir"/capacity || return | ||
38 | case "$capacity" in | ||
39 | '' | *[^0-9]*) | ||
40 | echo "Error: unexpected value for battery capacity: $capacity" >&2 | ||
41 | return 1 | ||
42 | ;; | ||
43 | esac | ||
44 | |||
45 | if [ "$capacity" -lt "$capacity_threshold" ] | ||
46 | then | ||
47 | systemctl suspend | ||
48 | fi | ||
49 | } | ||
50 | |||
51 | 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 @@ | |||
1 | [Unit] | ||
2 | Description = Suspend machine when battery capacity drops below threshhold | ||
3 | |||
4 | [Service] | ||
5 | ExecStart = suspend-below-battery-capacity 50 | ||
6 | Restart = on-failure | ||
7 | |||
8 | [Install] | ||
9 | WantedBy = default.target | ||