bat: issue: Battery percentage setting doesn't persist; systemctl does not indicate that bat@.service is running.

Subject of the issue

Hi, I’ve downloaded and used the bat file from the Release page, version 0.13, following your instruction to the letter but the setting doesn’t persist after restarting.

After running the bat, this is the services I see in systemctl:

Screenshot from 2023-09-17 20-21-51

When I run the following command in the Terminal after restarting:

systemctl --state=active --type=service

I do not see bat@.service running.

Screenshot from 2023-09-17 19-56-05

However, when I check for all services:

systemctl --type=service

I see that there’s the service bat@multi-user.service, but not activated Screenshot from 2023-09-17 19-55-53

The service file is identical to that in your source code.

Your system

OS: Ubuntu 22.04.3 LTS x86_64 Host: ZenBook UX425EA_UX425EA 1.0 Kernel: 6.2.0-32-generic Shell: bash 5.1.16 Resolution: 1920x1080 DE: Unity 7.6.0 WM: Compiz Terminal: gnome-terminal

Version

0.13 (Latest)

Steps to reproduce

Download and run the bat file as per instructions, then restart/shutdown the laptop.

Expected behaviour

After restart/turn on, the service should automatically be activated by the systemd and the systemctl.

Running bat threshold should return the pre-determined value.

Actual behaviour

The service isn’t run, not after restarting. Running bat threshold returns the default value (100).

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 44 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Ah. I see. Then it might better to do in the unit file template since the setup logic already resolves the actual value.

after I commanded sudo bat persist, the contents of the bat@.service reverted back to the previous version. When I try to enable the service with systemctl, this is what it said:

Failed to get properties: Unit name bat@.service is neither a valid invocation ID nor unit name.

That line doesn’t work because sh doesn’t expand the question mark in echo 60 >/sys/class/power_supply/BAT?/charge_control_end_threshold. You have to do: b=$(echo /sys/class/power_supply/BAT?/charge_control_end_threshold); echo 80 >"$b" (or you have to substitute the actual value into the unit files, so BAT0 or BAT1 or whatever).

Of course NixOS allows you to do basic operations like this. Their focus is on package & configuration management. They use systemd, they have bash.

@cloudmadeofcandy, please let me know if the following works.

This works perfectly. Thank you so much 💯

Yes, but being immutable and all, does it allow changing the threshold manually or otherwise?

replacing %s with /bin/bash does though.

I just use /bin/sh as I assume that to be universally available on Linux.

I’m not sure that’s the case on NixOS but that could be a good default.

@cloudmadeofcandy are you sure you’re running the correct version? bat@multi-user shouldn’t be there anymore. There should be 5 different services for each event after the change.

I haven’t merged the changes yet. They’re in this PR if you want to checkout the code.

Yes I left the %s in there so that the application doesn’t have to contain logic to find the shell. systemd can take care of that. Does it not work for you?

@tshakalekholoane It is also good to include RemainAfterExit=true in the unit files under [Service], because then the service shows as Active when doing systemctl status.

That confirms there is something wrong that @tshakalekholoane needs to fix.

@pepa65 you are correct.

After I turned the laptop on, the battery indicator turned red again, but when the ubuntu shell appeared, the battery indicator turned white again. This is a most peculiar turn of event.

The bat@.service is still indirect enabled, tho. What is happening and how do I fix it? The glob should work, right?

Should I close the issue when it is not fully solved?

Alright, I’ll try that.

systemctl list-unit-files I see my bat with enabled enabled both in green

Mine’s indirect enabled

Screenshot from 2023-09-17 21-19-59

On your system, what is the output of cat /etc/systemd/system/bat*

It is identical to the source file.

[Unit]
Description=Persist the battery charging threshold after %i
After=%i.target
StartLimitBurst=0

[Service]
Type=oneshot
Restart=on-failure
ExecStart=%s -c 'echo 60 > /sys/class/power_supply/BAT?/charge_control_end_threshold'

[Install]
WantedBy=%i.target

There really is no difference.

Looking at the %i and the description in the picture, %i seems to be equal to “multi-user”

I also notice that the battery limiter also works after shutting down, however when I turn it on, the threshold immediately resets to 100