Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not working if the computer is put to sleep mode #3

Closed
sjv-afk1 opened this issue Mar 9, 2021 · 36 comments
Closed

not working if the computer is put to sleep mode #3

sjv-afk1 opened this issue Mar 9, 2021 · 36 comments

Comments

@sjv-afk1
Copy link

sjv-afk1 commented Mar 9, 2021

If the laptop is put to suspend, then this feature doesn't work. Also, the persist function doesn't work.

And for example if I use the following command : ./bat --threshold 60, I get asked for root password, but I always get authentication failure. So, I need to use SUDO with it.

This program only works temporarily( During the session). If subjected to suspend or restart, then it fails.

I am using asus tuf a17 FA-706IH
operating system: ubuntu 20.04
kernel: 5.8.0-44-generic

@tshakalekholoane
Copy link
Owner

tshakalekholoane commented Mar 9, 2021

Thank you for bringing up the issue. I will have a look into it.

Just to make sure that I understand,

  1. when the laptop is put to suspend (or shut down), the charging threshold is not set anymore upon restart,
  2. the persist argument does not work at all,
  3. when you run bat --threshold 60 and get asked for the root password you get an authentication failure message but if you use sudo bat --threshold 60 it works?

@sjv-afk1
Copy link
Author

sjv-afk1 commented Mar 9, 2021

Thank you for bringing up the issue. I will have a look into it.

Just to make sure that I understand,

  1. when the laptop is put to suspend (or shut down), the charging threshold is not set anymore upon restart,
  2. the persist argument does not work at all,
  3. when you run bat --threshold 60 and get asked for the root password you get an authentication failure message but if you use sudo bat --threshold 60 it works?
  1. YES ( I mean sleep mode when I say suspend) So, if I resume from sleep mode, then threshold is reset to 100. ( However, in the command line it shows that threshold is 60, but I can see my laptop charging towards full capacity)
  2. YES
  3. YES

@tshakalekholoane
Copy link
Owner

Do you get any message after running sudo bat --persist?

@sjv-afk1
Copy link
Author

Do you get any message after running sudo bat --persist?

no

@tshakalekholoane
Copy link
Owner

tshakalekholoane commented Mar 10, 2021

Can you run the following command, test -f /etc/systemd/system/bat.service && echo "TRUE" and tell me what you get out?

@sjv-afk1
Copy link
Author

Can you run the following command, test -f /etc/systemd/system/bat.service && echo "TRUE" and tell me what you get out?

~/Binaries/bat1$ test -f /sys/class/power_supply/BAT?/charge_control_end_threshold && echo "TRUE"
TRUE

@tshakalekholoane
Copy link
Owner

Sorry about that, just edited the comment to run a different command. Can you run this command test -f /etc/systemd/system/bat.service && echo "TRUE"?

@sjv-afk1
Copy link
Author

Sorry about that, just edited the comment to run a different command. Can you run this command test -f /etc/systemd/system/bat.service && echo "TRUE"?

test -f /etc/systemd/system/bat.service && echo "TRUE"
TRUE

@tshakalekholoane
Copy link
Owner

Please check if the file has any contents by running, less /etc/systemd/system/bat.service.

If it does, then run systemctl enable bat.service and tell me what happens. I have a feeling on Ubuntu you might have to run this with sudo but let me know what happens in the first instance (without sudo) and if everything works after running these.

@sjv-afk1
Copy link
Author

less /etc/systemd/system/bat.service

Please check if the file has any contents by running, less /etc/systemd/system/bat.service.

If it does, then run systemctl enable bat.service and tell me what happens. I have a feeling on Ubuntu you might have to run this with sudo but let me know what happens in the first instance (without sudo) and if everything works after running these.

This is what I am getting after running first command:
[Unit]
Description=Set the battery charging threshold
After=multi-user.target
StartLimitBurst=0

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

[Install]
WantedBy=multi-user.target

~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
(END)

and after running "systemctl enable bat.service" nothing happens. I tried using SUDO also.

@tshakalekholoane
Copy link
Owner

Nothing happens as in there is no output after running the command or the battery threshold is still not persisting between restarts?

Can you run the following and send me the output? I might have to get Ubuntu installed to try to work this one out.

sudo bat -t 60

sudo bat -p

systemctl status bat.service

And just to make sure, you are running the latest version from the releases page right?

@sjv-afk1
Copy link
Author

systemctl status bat.service

Nothing happens as in there is no output after running the command or the battery threshold is still not persisting between restarts?

Can you run the following and send me the output? I might have to get Ubuntu installed to try to work this one out.

sudo bat -t 60

sudo bat -p

systemctl status bat.service

And just to make sure, you are running the latest version from the releases page right?

Yes I am running the latest release. The following is the output:

sjv@dh1729:/Binaries/bat1$ sudo ./bat -t 60
Identified face as sjv
Charging threshold set to 60.
sjv@dh1729:
/Binaries/bat1$ sudo ./bat -p
sjv@dh1729:~/Binaries/bat1$ systemctl status bat.service
● bat.service - Set the battery charging threshold
Loaded: loaded (/etc/systemd/system/bat.service; enabled; vendor preset: e>
Active: inactive (dead) since Mon 2021-03-08 15:25:59 IST; 2 days ago
Main PID: 2554 (code=exited, status=0/SUCCESS)

Mar 08 15:25:59 dh1729 systemd[1]: Starting Set the battery charging threshold.>
Mar 08 15:25:59 dh1729 systemd[1]: bat.service: Succeeded.
Mar 08 15:25:59 dh1729 systemd[1]: Finished Set the battery charging threshold.
lines 1-8/8 (END)...skipping...
● bat.service - Set the battery charging threshold
Loaded: loaded (/etc/systemd/system/bat.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Mon 2021-03-08 15:25:59 IST; 2 days ago
Main PID: 2554 (code=exited, status=0/SUCCESS)

Mar 08 15:25:59 dh1729 systemd[1]: Starting Set the battery charging threshold...
Mar 08 15:25:59 dh1729 systemd[1]: bat.service: Succeeded.
Mar 08 15:25:59 dh1729 systemd[1]: Finished Set the battery charging threshold.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
lines 1-8/8 (END)

@tshakalekholoane
Copy link
Owner

Can you check if this fix works?

bat.zip

@sjv-afk1
Copy link
Author

Can you check if this fix works?

bat.zip

Not working. Do I need to restart the computer and test it again?

@tshakalekholoane
Copy link
Owner

I think so. Judging by your output, everything should be working. The threshold level seems to be set to 91 and it should persist between restarts and sleep/suspend but not hibernation.

The only problem seems to have been with the call to set the threshold level which on Ubuntu didn't work because the root user appears to be locked by default but that should not be a problem with this version.

As a last resort, run sudo bat --reset, restart your computer, run sudo bat -t 60. If that does not work, show me the output to that along with the output to cat /sys/class/power_supply/BAT?/charge_control_end_threshold.

If that worked then, run sudo bat -p and show me the output to cat /etc/systemd/system/bat.service and systemctl status bat.service.

@sjv-afk1
Copy link
Author

Okay, I will post it in few hours.

@sjv-afk1
Copy link
Author

sjv-afk1 commented Mar 14, 2021

cat /sys/class/power_supply/BAT?/charge_control_end_threshold

sjv@dh1729:/Binaries/bat2$ sudo ./bat -t 60
Identified face as sjv
Charging threshold set to 60.
sjv@dh1729:
/Binaries/bat2$ sudo ./bat --persist
sjv@dh1729:~/Binaries/bat2$ cat /sys/class/power_supply/BAT?/charge_control_end_threshold
60

so after resuming from suspend, it is not working.

@tshakalekholoane
Copy link
Owner

Okay. That narrows it down. Without doing anything else, when you shut it down and boot again, is it still set to 60?

@sjv-afk1
Copy link
Author

Okay. That narrows it down. Without doing anything else, when you shut it down and boot again, is it still set to 60?

Yes, it shows that it is set to 60.

@tshakalekholoane
Copy link
Owner

Let me know if this fixes the problem with the threshold not persisting after sleep/suspend. But first run sudo rm /etc/systemd/system/bat.service.

@sjv-afk1
Copy link
Author

Let me know if this fixes the problem with the threshold not persisting after sleep/suspend. But first run sudo rm /etc/systemd/system/bat.service.

Didn't work.

@tshakalekholoane
Copy link
Owner

Seems to be an issue with systemd. Can you check which version is installed on your system by running systemctl --version?

tshakalekholoane pushed a commit that referenced this issue Mar 26, 2021
Set the charging threshold directly instead of running a shell command
prefixed with `su -c` because on some distributions the root user is
disabled by default.

Fixes parts of the following issues:
    - #3
    - #5
tshakalekholoane pushed a commit that referenced this issue Mar 26, 2021
Creates an additional systemd service that enables the charging
threshold to persist after the system goes into hibernation.

Also addresses parts of the following issues by adding checks for the
required systemd version,
    - #3
    - #5
tshakalekholoane pushed a commit that referenced this issue Mar 26, 2021
Creates an additional systemd service that enables the charging
threshold to persist after the system goes into hibernation.

Also addresses parts of the following issues by adding checks for the
required systemd version,
    - #3
    - #5
@sjv-afk1
Copy link
Author

sjv-afk1 commented Apr 2, 2021

systemctl --version
systemd 245 (245.4-4ubuntu3.4)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

@tshakalekholoane
Copy link
Owner

Curious. I’ll have to get a distribution of Ubuntu to understand exactly what is going on but it could take a while.

@agilum
Copy link

agilum commented Apr 9, 2021

Hi,

I am using ubuntu20.4+kernel 5.10+systemd 245 (245.4-4ubuntu3.6).

In may case bat-h, bat-c, bat-s works as it should
bat -t or sudo bat -t exit status 1
bat -p or sudo bat -p exit status 1 and service not starting
bat - r ok after sudo password

Can You help?

@tshakalekholoane
Copy link
Owner

Difficult to say right now. But I will look into this and post here as soon as I manage to get a distribution of Ubuntu.

@PrathamBhatTech
Copy link

PrathamBhatTech commented Jun 3, 2021

I have the same problem. Looking at your previous suggestions i have run the following:

test -f /etc/systemd/system/bat.service && echo "TRUE"
Returns nothing
less /etc/systemd/system/bat.service
Returns "no such file or directory"
systemctl --version
Returns the verision of systemd as 247

Ubuntu 21.04

@tshakalekholoane
Copy link
Owner

Cheers. I have had exams so I haven't had the time to work on a fix yet but I should be able to download Ubuntu and get to working on it in the comings weeks.

@tshakalekholoane
Copy link
Owner

tshakalekholoane commented Jun 10, 2021

Here is a proposed fix.

bat.zip

Let me know if it works.

For everyone else, if you could show me the output of the following command,

test -f /sys/class/power_supply/BAT?/charge_control_end_threshold && echo "TRUE"

Ubuntu 20.04 does not seem to expose the charging threshold variable. At least when running it in a VM. But if you get an output value of TRUE to the above command then I can look further into it.

@PrathamBhatTech
Copy link

PrathamBhatTech commented Jun 10, 2021

Hi,
I think I might need a clarification on the difference between suspend and hibernate.

I tried the new bat file and it's the same as before. If i close the lid of the laptop and open it the laptop starts charging. bat -t returns 60.
When the lid is closed the battery is stopped at 60 but when i open the lid it starts charging. Specifically after logging in. Then i have to type sudo bat -t 60 again and I did sudo bat -p

test -f /sys/class/power_supply/BAT?/charge_control_end_threshold && echo "TRUE"
This code returns TRUE for me.

Oh and when i reboot it stops at 60. The only time it stops is when the lid is closed or when i leave for too long.

To try the new bat file I deleted using sudo rm /usr/local/bin/bat then sudo install bat /usr/local/bin after replacing the old bat file.

Ubuntu 20.04 LTS

@tshakalekholoane
Copy link
Owner

tshakalekholoane commented Jun 10, 2021

Suspend stores the machine's state in RAM and keeps it in low power mode whereas hibernate saves the machine's state in swap space and completely turns it off.

Hibernate is notoriously difficult to get right on Linux and might even be disabled on Ubuntu by default but with some additional configuration you could enable it.

Anyway, try the following and let me know if it fixes the problem.

bat.zip

Just remember to run the --reset command first and make sure you're running this build using the --version flag which should say 0.6-beta.

@PrathamBhatTech
Copy link

It works great.
Thank you for all the help.

@tshakalekholoane
Copy link
Owner

Thank you for feedback! Can you post the output of the following command so that I can finalise a release?

systemctl status bat-sleep.service

@PrathamBhatTech
Copy link

PrathamBhatTech commented Jun 12, 2021

systemctl status bat-sleep.service

Output (When the charging is stopped at 60%):

● bat-sleep.service - Persist the battery charging threshold after hibernation
Loaded: loaded (/etc/systemd/system/bat-sleep.service; enabled; vendor preset: enabled)
Active: inactive (dead)

@tshakalekholoane
Copy link
Owner

Thank you.

tshakalekholoane pushed a commit that referenced this issue Jun 12, 2021
Fix an issue (#3) on some
distributions, most notably Ubuntu, where the charging threshold would
not persist after the system went into suspend.

This also updates the documentation and refactors the code to handle
errors more effectively.
tshakalekholoane pushed a commit that referenced this issue Jun 12, 2021
Fix an issue (#3) on some
distributions, most notably Ubuntu, where the charging threshold would
not persist after the system went into suspend.

This also updates the documentation and refactors the code to handle
errors effectively.
tshakalekholoane pushed a commit that referenced this issue Jun 20, 2021
Fix an issue (#3) on some
distributions, most notably Ubuntu, where the charging threshold would
not persist after the system went into suspend.

This also updates the documentation and refactors the code to handle
errors better.
@igor-frolikov
Copy link

igor-frolikov commented Jul 24, 2021

bat 0.7
works fine after restart and return from suspend, but fails after return from hibernate. Resumes working fine after restart.
I tried install from github prebuilt images and from aur via pamac - no any difference.

OS: Manjaro Linux [x86_64]
Host: VivoBook_ASUSLaptop X513IA_M513IA 1.0
Kernel: 5.10.52-1-MANJARO
Packages: 1277 (pacman)[stable]
Shell: bash 5.1.8
DE: KDE Plasma 5.22.3
WM: KWin (X11)
CPU: AMD Ryzen 5 4500U with Radeon Graphics (6) @ 2.375GHz
GPU: AMD ATI Renoir
Memory: 1720MiB / 15484MiB (11%)
Disk (/): 12GB / 100GB (12%)
Battery: 90% [Not charging]

PS.
Thank you very much for your efforts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@agilum @tshakalekholoane @PrathamBhatTech @sjv-afk1 @igor-frolikov and others