2

Running Ubuntu 20.04.

In order to conserve battery life, I have a service that runs at boot that will cap the charge level to 80%, see service below (/etc/systemd/system/battery-charge-threshold.service):

[Unit]
Description=Set the battery charge threshold
After=multi-user.target
StartLimitBurst=0

[Service] Type=oneshot Restart=on-failure ExecStart=/bin/bash -c 'echo 80 > /sys/class/power_supply/BAT0/charge_control_end_threshold'

[Install] WantedBy=multi-user.target

This works well, and is even sustained after shutdown of my computer. During the initial boot of the computer BIOS/UEFI, the setting seem to be reset to 100% (indicated by charging LED indicator on the laptop switching from "Full" to "Charging" light). However, once Ubuntu is booted, the service runs and the limit is again set to 80%. The setting is sustained in s2idle and resume from s2idle, but when resuming from hibernate the service does not seem to be run, and the limit is thus at 100%. After resume, I can activate the service using

sudo systemctl start battery-charge-threshold.service

and everything works again. However, I would like to have the service run automatically at resume from hibernate. I have seen how to make this happen on resume from s2idle or suspend, but I have not been able to find anything on how to make this happen with hibernate.

Anyone know how to approach this?

1 Answers1

1

Answering my own solution here:

The approach was in fact the same as adding functions to resume from suspend, I must have made errors on my earlier tries.

This is how I did it:

Following the first answer from this question, I added a script called batt_script.sh in /lib/systemd/system-sleep:

#!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin

case "$1" in pre) #code execution BEFORE sleeping/hibernating/suspending ;; post) #code execution AFTER resuming sudo systemctl start battery-charge-threshold.service ;; esac

exit 0

Then choose allow executing file as program from nautilus admin:// to gain root permissions or with

sudo chmod +x /lib/systemd/system-sleep/batt-script.sh

The command sudo systemctl start battery-charge-threshold.service in the post-section of the script is what runs the service, and it happens just before the login screen appears (indicated by the charge state LED indicator switching from Charging to Full)

I am happy I solved this, hopefully this can help someone else.

zx485
  • 2,426