You are not logged in.
Pages: 1
I have a python script that I ran as a cron job to notify me when my battery was about to die. I wanted to change that cron job into a udev rule, but I cannot get it to work at all.
The main issue I faced is: It sometimes works and some other times it doesn't and I don't know how to replicate this issue.
How can I fix this?
Is there a way to manually trigger the udev event rather than waiting for my battery to charge then die and repeat? This would at least help me replicate the issue and figure out whats wrong
This is my udev rules file
# Rule to send a notification when battery is below 20%
# This one sometimes work
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-1][0-9]", ENV{DISPLAY}=":0", RUN+="/usr/bin/su gxhary -c '/home/gxhary/.config/scripts/battery_watcher.py battery-low'"
# these two do work at exact values
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="20", ENV{DISPLAY}=":0", RUN+="/usr/bin/su gxhary -c '/home/gxhary/.config/scripts/battery_watcher.py battery-low'"
# Rule to send a notification when battery is fully charged
SUBSYSTEM=="power_supply", ATTR{status}=="Charging", ATTR{capacity}=="100", ENV{DISPLAY}=":0", RUN+="/usr/bin/su gxhary -c '/home/gxhary/.config/scripts/battery_watcher.py battery-charged'"Another issues I faced is I can't get it to work at 20% or 100%, (exact values) I can only get it to work (when it does work) at ranges.
Thanks in advance
Last edited by gxhary (2026-01-26 03:04:16)
Offline
See the note in https://wiki.archlinux.org/title/Laptop#udev - if the battery only emits discharge events < 5% but dies at 10% you'll never see those and polling will unfortunately remain your only option ![]()
Offline
See the note in https://wiki.archlinux.org/title/Laptop#udev - if the battery only emits discharge events < 5% but dies at 10% you'll never see those and polling will unfortunately remain your only option
No that's the thing, it occasionally works, but it is not consistent. So my battery does send those events, but the udev rules arent always properly triggered
Offline
Does it really ever trigger on capacity == "09" (rather than just "9")?
Did you test whether the udev rule fires or whether your script does the expected thing?
Try a rule that does sth. like "/usr/bin/touch /tmp/udev.fired" to track the invocation.
Offline
Does it really ever trigger on capacity == "09" (rather than just "9")?
Did you test whether the udev rule fires or whether your script does the expected thing?
Try a rule that does sth. like "/usr/bin/touch /tmp/udev.fired" to track the invocation.
When my battery is 9% it does trigger, so my guess is yes it triggers at 09 because that's the only rule I have ([0-1][0-9]).
It does not work when I try to match a single number like 20 or 100.
My script works as expected, I ran it outside the udev rule and it works fine.
I am also using the same exact script as a udev rule for when plugging and unplugging the charger and it works fine.
The problem is not my script.
The problem is udev rules for when the battery is discharging occasionally works.
I do not understand why and I cannot replicate it.
- Is there a way to trigger the udev rules manually? I would be able to debug it on my own.
- If not then how can I properly test it?
- Is my syntax correct for the single number matching? I couldn't something helpful for it. Because apparently
ATTR{capacity}=="20"is not correct
Offline
It does not work when I try to match a single number like 20 or 100.
Specific percentage might be skipped?
My script works as expected, I ran it outside the udev rule and it works fine.
So it's just useless…
Track what's actually going on.
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/tmp/chargelog.sh '$attr{capacity}'"/tmp/chargelog.sh
#!/bin/sh
echo $1 >> /tmp/charge.logwill hopefully do this (I don't get discharge events > 8% or so and am currently at 98& w/ a remaining time > 10h
Offline
It does not work when I try to match a single number like 20 or 100.
Specific percentage might be skipped?
My script works as expected, I ran it outside the udev rule and it works fine.
So it's just useless…
Track what's actually going on.SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/tmp/chargelog.sh '$attr{capacity}'"/tmp/chargelog.sh
#!/bin/sh echo $1 >> /tmp/charge.logwill hopefully do this (I don't get discharge events > 8% or so and am currently at 98& w/ a remaining time > 10h
Thank you I will do this and report back, hopefully this helps me fix it
Offline
It does not work when I try to match a single number like 20 or 100.
Specific percentage might be skipped?
My script works as expected, I ran it outside the udev rule and it works fine.
So it's just useless…
Track what's actually going on.SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/tmp/chargelog.sh '$attr{capacity}'"/tmp/chargelog.sh
#!/bin/sh echo $1 >> /tmp/charge.logwill hopefully do this (I don't get discharge events > 8% or so and am currently at 98& w/ a remaining time > 10h
I tried it and I wasn't getting discharge events at all.
Unless I can get it to send events, I guess I will just stick with a cron job
Thank you for your help
Offline
Then how do you previously receive events?
Forgot to xhmod ugo+x /tmp/chargelog.sh ?
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/usr/bin/touch /tmp/discharge.proof"Offline
Gxhary, make sure you’re actually matching the right thing. More precisely: that you’re selecting the most specific device.
Events for a single device usually have multiple matches at different levels of specificity. The less specific ones may be missing some attributes you’re testing against. `udevadm monitor` and `udevadm info --attribute-walk` are your friends.
On the part regarding matching exact values: do not. This is nothing with udev, it’s sampling 101. Intermediate values may be missing in a sample of a varying function. Always test for ranges. It’s going to be less tedious, if that’s being done in the script itself. You have the freedom of using more complex logic in a script.
Paperclips in avatars? | Sometimes I seem a bit harsh — don’t get offended too easily!
Offline
Fwwi, #6 works for me (at least I get a single discharge when unplugging the power and it logs 100, again - don't expect further messages > 8% or so)
Offline
Then how do you previously receive events?
Forgot to xhmod ugo+x /tmp/chargelog.sh ?SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", RUN+="/usr/bin/touch /tmp/discharge.proof"
That's what i am saying, it's inconsistent.
What exactly happened is as follows:
1. I set the rule as you said
2. I tested the script with another rule that I know works and can easily replicate
3. I used my laptop normally until it died
4. I frequently checked the log file and saw nothing
5. Interestingly, when I closed the laptops lid to go from the living room to my bedroom, I noticed it did log, somehow that means that I get events when my laptop is idle?
6. I only got events when I closed my lid and opened it back, but not when I was normally using it
`udevadm monitor` and `udevadm info --attribute-walk` are your friends.
does that mean i can run `udevadm monitor` and see the discharge events when/if they happen?
Update: I ran `udevadm monitor` till my laptop died and still got nothing ![]()
Last edited by gxhary (Yesterday 03:21:28)
Offline
At what percentage does your battery typically die?
Offline
mpan wrote:`udevadm monitor` and `udevadm info --attribute-walk` are your friends.
does that mean i can run `udevadm monitor` and see the discharge events when/if they happen?
It’s going to report all events that udev sees, reporting the device path for each. If events happen in relation to your laptop battery, the associated device should appear there. You can then analyze, what attributes and at which level of specificity are available.
Update: I ran `udevadm monitor` till my laptop died and still got nothing
Wait? Absolutely no output?
Paperclips in avatars? | Sometimes I seem a bit harsh — don’t get offended too easily!
Offline
At what percentage does your battery typically die?
Normally at like 1% or 0%
Wait? Absolutely no output?
No output related to the battery, I got some for other things like changing my brightness and plugging/unplugging my charger
Offline
So it's not clear whether the rule has actually ever worked or the script invocation was external?
=> https://archlinux.org/packages/extra/x86_64/batsignal/
Offline
So it's not clear whether the rule has actually ever worked or the script invocation was external?
I think I figured out what is happening. I think I only get discharge events when I unplug my charger.
I been trying to figure it out and I realized I only get a notification when I unplug my charger.
I was able to get it consistently to work by plugging in the charger, waiting a few seconds, then unplugging it.
I would consistently get the rule to work.
I guess this means that my batter doesn't send discharge events at certain percentages, rather it sends it when it starts discharging.
This is the udevadm monitor output when I was able to replicate it
[gxhary@archlinux ~]$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
KERNEL[55247.213898] change /devices/platform/ACPI0003:00/power_supply/AC (power_supply)
KERNEL[55247.254560] change /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
UDEV [55247.263351] change /devices/platform/ACPI0003:00/power_supply/AC (power_supply)
UDEV [55248.329342] change /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0A:00/power_supply/BAT0 (power_supply)Thank you for the help
Edit: Also Thank you for linking the daemon, my plan was to get the udev rules to work, my backup was to make my own daemon. I'll take some inspiration from it and make my own in rust lol, thanks
Last edited by gxhary (Yesterday 21:49:38)
Offline
Pages: 1