Custom systemd service to set battery charging threshold fails to start at startup

My device is Asus VivoBook S531FL. On Windows, MyAsus app allows to set battery charging threshold(e.g. 60%, 80%, 100%). There is no option to set this limit in BIOS. I created a systemd service to set the desired threshold. It used to work fine. But after recent updates to Manjaro and KDE Plasma, it stopped working. It says [FAILED] Failed to start battery-charge-threshold.service at startup before login screen comes up. I tried reloading the daemon and restarting the service. But no luck. I have also tried deleting the battery-charge-threshold.service and recreating it. But nothing works. Please help me to resolve this problem. I am very new to Manjaro(Arch). I never encountered this issue on Debian based distros.

Steps I followed to create the systemd service:
1.

sudo nano /etc/systemd/system/battery-charge-threshold.service

  1. Wrote following lines in the file

[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

sudo systemctl enable battery-charge-threshold.service
sudo systemctl start battery-charge-threshold.service

  1. To reload I used

sudo systemctl daemon-reload
sudo systemctl restart battery-charge-threshold.service

What’s the error message of the service?

systemctl status battery-charge-threshold.service

Or

journalctl -u battery-charge-threshold.service
1 Like

Use valid path

/usr/lib/systemd/system/battery-charge-threshold.service

NOT /etc/systemd/ where are created symlinks.

That’s wrong. /usr is for package-provided files, /etc is for files from the system administrator. Source: systemd - ArchWiki

1 Like
  1. For

systemctl status battery-charge-threshold.service

Output:

○ battery-charge-threshold.service - Set the battery charge threshold
Loaded: loaded (/etc/systemd/system/battery-charge-threshold.service; enabled; vendor p>
Active: inactive (dead) since Sun 2021-08-22 05:06:18 +06; 1min 38s ago
Process: 666 ExecStart=/bin/bash -c echo 80 > /sys/class/power_supply/BAT0/charge_contro>
Main PID: 666 (code=exited, status=0/SUCCESS)
CPU: 6ms

Aug 22 05:06:18 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 22 05:06:18 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 22 05:06:18 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
lines 1-10/10 (END)

  1. For

journalctl -u battery-charge-threshold.service

Output:

– Journal begins at Tue 2021-08-03 13:18:31 +06, ends at Sun 2021-08-22 05:08:57 +06. –
Aug 04 07:12:38 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 04 07:12:38 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 04 07:12:38 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot ee9980c3a69747558269a5d576db7fce –
Aug 04 12:03:38 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 04 12:03:38 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 04 12:03:38 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot 01063cb064e240c2b4bd4be56afc504f –
Aug 04 12:19:56 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 04 12:19:56 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 04 12:19:56 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot b0d3303c930143a48ed932dbc1564858 –
Aug 04 17:57:16 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 04 17:57:16 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 04 17:57:16 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot 1b11b852737940bdb618f2611fa4d56e –
Aug 04 20:32:46 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 04 20:32:46 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 04 20:32:46 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot 66edcbe5ddd342d1864fb261a7e7ee8e –
Aug 05 16:00:19 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 05 16:00:19 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 05 16:00:19 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot a90f62b313784d6f864e683289ba19f5 –
Aug 05 21:19:47 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 05 21:19:47 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 05 21:19:47 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot dea9a42f7c834283b730da41fbcee85c –
Aug 06 08:37:33 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 06 08:37:33 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 06 08:37:33 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot 9397c963121043c4beb59804e2ee9b23 –
Aug 06 12:24:06 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 06 12:24:06 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 06 12:24:06 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot 0b330d9491af4e229d80c518c8d9a701 –
Aug 06 13:47:36 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 06 13:47:36 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>
Aug 06 13:47:36 VivoBookS15 systemd[1]: Finished Set the battery charge threshold.
– Boot 9758b6059f4b4488a839d27492680c58 –
Aug 07 14:40:54 VivoBookS15 systemd[1]: Starting Set the battery charge threshold…
Aug 07 14:40:54 VivoBookS15 systemd[1]: battery-charge-threshold.service: Deactivated succes>

Also this comes up while booting:[please go to this address. The forum is not allowing to post any image or link] h t t p s : / / sajidifti . com / failed . jpg

The log you gave indicates that it works but in your screenshot it doesn’t.
The log of the failed case is important and you have to provide it.

What can I do now? It works sometimes, sometimes it doesn’t. When it works, the plasma battery icon says that although the power adapter is connected, the battery is still draining and the adapter is not providing enough power. I am confused and frustrated at this point.

By the way, thanks for looking into this.

Maybe the file is not created by the driver yet when the script is run? Have you tried putting adding some delay?

2 Likes

No. Good idea though. Will definitely try.

You were right. Adding some delay fixed the problem with 99% success rate. I have added 10 sec delay. The problem was caused by the slow launch of multi-user.target. I have added the following line to my original script

[Service]
ExecStartPre=/bin/sleep 10

It fixed the [FAILED] Failed to start battery-charge-threshold.service issue at startup. It also fixed the misbehavior of the service after booting into the desktop. However, plasma battery icon sometimes says that Battery at 80% But Still Discharging. The Power Supply Is Not Powerful Enough To Charge The Battery.. This is no biggie. I am happy that the service is working.

Thank you very much

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.