You are not logged in.

#1 2018-08-25 10:52:06

Miodvitnir
Member
Registered: 2018-08-25
Posts: 17

[Solved] Script to suspend on low battery level

Hello,

I am new here and I thank you for welcoming me. I am french, so please excuse my imperfect english.
So, I would like my computer to suspend when my battery is on low battery.
I followed the wiki here
Sadly, the systemd's solution did not work, probably, as precised in the wiki, because there is no report events from my battery.
I also tried cron, and that did not work too.
I looked on the gentoo wiki for cron, and verified as proposed with this script :
* * * * * /bin/echo "foobar" >> /file_you_own
So as to verify if cron is working, and it is the case, the file created is been editing every minute.
So there might be a problem with the script from the laptop wiki, or with the way I used it.
I put it in :
/etc/cron.d/battery.sh
Here is what it contains :

#!/bin/sh
acpi -b | awk -F'[,:%]' '{print $2, $3}' | {
	read -r status capacity

	if [ "$status" = Discharging -a "$capacity" -lt 5 ]; then
		logger "Critical battery threshold"
		systemctl hibernate
	fi
}

Here is my crontab :

* * * * * /etc/cron.d/battery.sh

And crontab -l also display this script.

Thanks for helping me smile

Last edited by Miodvitnir (2018-08-26 12:03:28)

Offline

#2 2018-08-25 11:48:55

ooo
Member
Registered: 2013-04-10
Posts: 1,638

Re: [Solved] Script to suspend on low battery level

Miodvitnir wrote:

Sadly, the systemd's solution did not work, probably, as precised in the wiki, because there is no report events from my battery.

Did you actually test that your battery is not reporting discharge events, or is this only a guess? A testing method is explained on the wiki page:

Note: Not all batteries report discharge events. Test by running udevadm monitor --property while on battery and see if any events are reported. You should wait at least 1% drop. If no events are reported and /sys/class/power_supply/BAT0/alarm is non-zero then the battery will likely trigger an event when BAT0/energy_now drops below the alarm value, and the udev rule will work as long as the percentage math works out. Some laptops have an option for this disabled in BIOS by default.

If there are no events, did you test that your battery.sh script works by running it manually outside of cron?

Offline

#3 2018-08-25 12:34:45

Miodvitnir
Member
Registered: 2018-08-25
Posts: 17

Re: [Solved] Script to suspend on low battery level

Now that you are saying it, I had test that, and I remember there actually were a message one second before the shutting down of the computer from empty battery. And now I try that again, there is a message while I unplug the cable. Though there is still the rule, from the wiki, for udev in my /etc/udev/rules.d which do not do anything, but maybe there is something I ignore or miss?

Yes, I tested the bash script manually and it works.

Last edited by Miodvitnir (2018-08-25 13:03:39)

Offline

#4 2018-08-25 13:27:47

ooo
Member
Registered: 2013-04-10
Posts: 1,638

Re: [Solved] Script to suspend on low battery level

Can you post the output of the following command, while on battery:

# cat /sys/class/power_supply/BAT*/{status,capacity,alarm,energy_now}

And also the output of

# acpi -b | awk -F'[,:%]' '{print $2, $3}'

Did you test that the udev rule itself works, e.g. as instructed on the laptop wiki page?

Offline

#5 2018-08-25 13:33:52

Miodvitnir
Member
Registered: 2018-08-25
Posts: 17

Re: [Solved] Script to suspend on low battery level

# cat /sys/class/power_supply/BAT*/{status,capacity,alarm,energy_now} output :

 Discharging
69
0
cat: '/sys/class/power_supply/BAT*/energy_now': Aucun fichier ou dossier de ce type (which means : no file or folder of this type)

And the # acpi -b | awk -F'[,:%]' '{print $2, $3}' one :

Discharging  68

Offline

#6 2018-08-25 14:04:03

Miodvitnir
Member
Registered: 2018-08-25
Posts: 17

Re: [Solved] Script to suspend on low battery level

And I just saw your last question.
I just tried this command : udevadm test $(udevadm info -q path -n device_name) 2>&1
But each device name I tried (as BAT0) it tells me "device node not found" or "syspath not found", could you explain me please?

Offline

#7 2018-08-25 14:26:30

ooo
Member
Registered: 2013-04-10
Posts: 1,638

Re: [Solved] Script to suspend on low battery level

For the udev rule to work, the battery needs to report some event while it's discharging and the charge has dropped below the 5% level. Some batteries send event from every charge percentage drop, which doesn't seem to happen on your system if you don't see those reports on udevadm monitor. Some batteries send alert when the charge level is below the threshold, but since /sys/class/power_supply/BAT0/alarm is 0, that doesn't seem to happen either.

Therefore, it's possible that the udev rule can't work on your system.

However I don't see any reason why the cron method wouldn't work. Could you clarify what you meant when you said that the battery.sh works when run manually? Does it actually hibernate your system when charge is below 5, or just that it runs without errors?

EDIT.

Miodvitnir wrote:

I just tried this command : udevadm test $(udevadm info -q path -n device_name) 2>&1
But each device name I tried (as BAT0) it tells me "device node not found" or "syspath not found", could you explain me please?

BAT0 is not correct device name. You can use e.g. '-p  /sys/class/power_supply/BAT0' instead of '-n /dev/whatever' See https://wiki.archlinux.org/index.php/Ud … f_a_device.

Last edited by ooo (2018-08-25 14:37:28)

Offline

#8 2018-08-25 14:50:40

Miodvitnir
Member
Registered: 2018-08-25
Posts: 17

Re: [Solved] Script to suspend on low battery level

Thank you for the explanations.

Actually, when I run :

bash battery.sh

That put my computer in hibernation if the charge is under the value indicate.
Well, actually that turns my computer down savagely, so i think there is a problem with the hibernation. I also changed the script to make it suspend, and it works.
I do not understand either, cronie works, the script too, why the every minute cron task don't?

Last edited by Miodvitnir (2018-08-25 14:52:54)

Offline

#9 2018-08-25 15:15:06

ooo
Member
Registered: 2013-04-10
Posts: 1,638

Re: [Solved] Script to suspend on low battery level

Is the battery.sh file executable?
What's the output of 'ls -l /etc/cron.d/battery.sh'

Anything in systemd journal from cron, when it tries to execute the script?

If you can't figure it out, you could always try if systemd timer worked any better over cron.

If you can't manually hibernate your system, that's a separate issue and you should open a new thread if you need assistance with that.

Also, I meant to point out earlier that you should use LC_ALL=C to get command output in English, if you're going to post the output to these forums: https://wiki.archlinux.org/index.php/Co … s_and_code

Last edited by ooo (2018-08-25 15:20:02)

Offline

#10 2018-08-26 06:07:26

Miodvitnir
Member
Registered: 2018-08-25
Posts: 17

Re: [Solved] Script to suspend on low battery level

Ok for LC_ALL=C, I will try to remember

I made a chmod 755, and after a chmod ugo+rwx as the script still not working, here the output of 'ls -l /etc/cron.d/battery.sh'

-rwxrwxrwx 1 root root 205 Aug 26 07:54 /etc/cron.d/battery.sh

I saw the systemd solution, but that seems less easy to configure, I will study that if there is no solution with cron, thanks.

One can see, in journalctl, only the logger messages :

août 26 08:03:01 dell antoine[5783]: Critical battery threshold
août 26 08:04:01 dell antoine[5838]: Critical battery threshold
août 26 08:05:01 dell antoine[5848]: Critical battery threshold
août 26 08:06:01 dell antoine[5858]: Critical battery threshold 

Offline

#11 2018-08-26 06:08:13

Miodvitnir
Member
Registered: 2018-08-25
Posts: 17

Re: [Solved] Script to suspend on low battery level

Need I to be the owner of the script so as to permit crontab to execute it?

EDIT : But the logger message won't be present... it is so nonsense, that is confusing me  roll

Last edited by Miodvitnir (2018-08-26 06:11:08)

Offline

#12 2018-08-26 08:46:55

ooo
Member
Registered: 2013-04-10
Posts: 1,638

Re: [Solved] Script to suspend on low battery level

Yes, the script is obviously working and runs every minute.

My guess is, that whatever user cron is running the script as, lacks permissions to suspend/hibernate with systemctl. Did you make sure you added the job to root user's crontab, and not your regular user's?

Offline

#13 2018-08-26 12:02:58

Miodvitnir
Member
Registered: 2018-08-25
Posts: 17

Re: [Solved] Script to suspend on low battery level

You guessed right, it was my crontab, not root's, I did not know that would be a problem.
Now it works, thanks a lot for your help smile

Offline

Board footer

Powered by FluxBB