Tip Of The Week: Controlling The Battery Charge Threshold

Came across this rather interesting reader comment <https://forums.theregister.com/forum/all/2022/02/18/something_for_the_weekend/#c_4417057> about (supposedly) prolonging the life of your laptop battery by reducing the limit to which it is charged. This is configured by creating a systemd service file (called, for example, /etc/systemd/system/battery-charge-threshold.service) that automatically sets the threshold at boot time. Its contents are [Unit] Description=Set the battery charge threshold After=multi-user.target StartLimitBurst=0 [Service] Type=oneshot Restart=on-failure ExecStart=/bin/bash -c 'echo 50 > /sys/class/power_supply/BAT0/charge_control_end_threshold' [Install] WantedBy=multi-user.target You then use the usual “systemctl start” command to invoke this immediately, plus “systemctl enable” to ensure it runs at every subsequent boot. Regardless of whether it actually prolongs the life of your battery (try at your own risk!), it shows how easy it is to customize aspects of your system behaviour with systemd. As of this writing, that comment has garnered 7 upvotes and no downvotes, on a site where the readers are notorious for being a nest of systemd-haters.

-----Original Message----- From: Lawrence D'Oliveiro <ldo(a)geek-central.gen.nz> Sent: Saturday, 19 February 2022 15:37 To: wlug(a)list.waikato.ac.nz Subject: [wlug] Tip Of The Week: Controlling The Battery Charge Threshold
Came across this rather interesting reader comment <https://forums.theregister.com/forum/all/2022/02/18/something_for_the _weekend/#c_4417057> about (supposedly) prolonging the life of your laptop battery by reducing the limit to which it is charged. This is configured by creating a systemd service file (called, for example, /etc/systemd/system/battery-charge-threshold.service) that automatically sets the threshold at boot time. Its contents are
Could these not also have gone into the relevant scripts for SysV or init.d startups? Or is /sys/class/power/BAT0 part of a systemd only API?

On Sat, 19 Feb 2022 07:59:36 +0000, Warren Boyd wrote:
Could these not also have gone into the relevant scripts for SysV or init.d startups?
Which ones?
Init.d: Create a script: ``` #!/bin/bash # chkconfig: 2345 # description: Set the battery charge threshold start() { echo 50 > /sys/class/power_supply/BAT0/charge_control_end_threshold } case "$1" in start) start ;; stop) restart) ;; *) echo "Usage: $0 {start|stop|restart}" esac exit 0 ``` And then add it to the system using `chkconfig` SysV - it could be as brutal as adding `echo 50 > /sys/class/power_supply/BAT0/charge_control_end_threshold &` as an extra line into /etc/rc.local I'm just trying to gauge how systemd makes this an easy customization of the system compared to what came before it. I'll concede that the script for systemd is a lot more readable - so perhaps that in its entirety is a win? Cheers!

On Sat, 19 Feb 2022 08:54:29 +0000, Warren Boyd wrote:
#!/bin/bash # chkconfig: 2345 # description: Set the battery charge threshold
start() { echo 50 > /sys/class/power_supply/BAT0/charge_control_end_threshold }
case "$1" in start) start ;; stop) restart) ;; *) echo "Usage: $0 {start|stop|restart}" esac
exit 0
That’s 20 lines (17 excluding blank ones), nearly all of which is repetitive boilerplate. You’ll note the one I posted was just 12, including blank lines, of which only one was actual executable code (all the rest were directives).
I'm just trying to gauge how systemd makes this an easy customization of the system compared to what came before it.
Hopefully that answers your question ...

-----Original Message----- From: Lawrence D'Oliveiro <ldo(a)geek-central.gen.nz> Sent: Saturday, 19 February 2022 22:06 To: wlug(a)list.waikato.ac.nz Subject: [wlug] Re: Tip Of The Week: Controlling The Battery Charge Threshold
On Sat, 19 Feb 2022 08:54:29 +0000, Warren Boyd wrote:
<snip unneeded repetition>
That’s 20 lines (17 excluding blank ones), nearly all of which is repetitive boilerplate. You’ll note the one I posted was just 12, including blank lines, of which only one was actual executable code (all the rest were directives).
I'm just trying to gauge how systemd makes this an easy customization of the system compared to what came before it.
Hopefully that answers your question ...
Cheers - sure does. I think I'm starting to show my age ;-)

On Sat, 19 Feb 2022 15:36:41 +1300, I wrote:
[Unit] Description=Set the battery charge threshold After=multi-user.target
Actually, I think this “After=” line should be removed. The reason is that
[Install] WantedBy=multi-user.target
this line means that the “multi-user” target is not fully reached until this particular service has started (which is the standard way to install your service into the multi-user state). Whereas “After=” indicates an ordering the other way round.
participants (2)
-
Lawrence D'Oliveiro
-
Warren Boyd