电池电量不足时休眠的 udev 规则不起作用

电池电量不足时休眠的 udev 规则不起作用

我一直在尝试设置一个 udev 规则,以便在电池电量不足时让笔记本电脑休眠。遵循指南可在 arch wiki 中找到我最终采用以下规则来测试它是否正常工作:

$ cat /etc/udev/rules.d/99-lowbat.rules 
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-99]", RUN+="touch /tmp/test_shutdown"

/tmp/test_shutdown由于尚未创建,因此规则根本没有被触发

udevadm 监视输出

$ udevadm monitor --property
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[6099.285997] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1b/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
ACTION=change
DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1b/PNP0C09:00/PNP0C0A:00/power_supply/BAT0
SUBSYSTEM=power_supply
POWER_SUPPLY_NAME=BAT0
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-poly
POWER_SUPPLY_CYCLE_COUNT=40
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=15360000
POWER_SUPPLY_VOLTAGE_NOW=15855000
POWER_SUPPLY_POWER_NOW=9877000
POWER_SUPPLY_ENERGY_FULL_DESIGN=80400000
POWER_SUPPLY_ENERGY_FULL=73880000
POWER_SUPPLY_ENERGY_NOW=51890000
POWER_SUPPLY_CAPACITY=70
POWER_SUPPLY_CAPACITY_LEVEL=Low
POWER_SUPPLY_MODEL_NAME=01AY969
POWER_SUPPLY_MANUFACTURER=Celxpert
POWER_SUPPLY_SERIAL_NUMBER=  675
SEQNUM=8036

UDEV  [6099.315142] change   /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1b/PNP0C09:00/PNP0C0A:00/power_supply/BAT0 (power_supply)
ACTION=change
DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1b/PNP0C09:00/PNP0C0A:00/power_supply/BAT0
SUBSYSTEM=power_supply
POWER_SUPPLY_NAME=BAT0
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-poly
POWER_SUPPLY_CYCLE_COUNT=40
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=15360000
POWER_SUPPLY_VOLTAGE_NOW=15855000
POWER_SUPPLY_POWER_NOW=9877000
POWER_SUPPLY_ENERGY_FULL_DESIGN=80400000
POWER_SUPPLY_ENERGY_FULL=73880000
POWER_SUPPLY_ENERGY_NOW=51890000
POWER_SUPPLY_CAPACITY=70
POWER_SUPPLY_CAPACITY_LEVEL=Low
POWER_SUPPLY_MODEL_NAME=01AY969
POWER_SUPPLY_MANUFACTURER=Celxpert
POWER_SUPPLY_SERIAL_NUMBER=  675
SEQNUM=8036
USEC_INITIALIZED=6099287730

udevadm 测试输出:

$ udevadm test /sys/class/power_supply/BAT0
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

Load module index
Parsed configuration file /usr/lib/systemd/network/99-default.link
Created link configuration context.
Reading rules file: /usr/lib/udev/rules.d/01-md-raid-creating.rules
Reading rules file: /usr/lib/udev/rules.d/10-dm.rules
Reading rules file: /usr/lib/udev/rules.d/11-dm-lvm.rules
/usr/lib/udev/rules.d/11-dm-lvm.rules:40 Invalid value for OPTIONS key, ignoring: 'event_timeout=180'
/usr/lib/udev/rules.d/11-dm-lvm.rules:40 The line takes no effect, ignoring.
Reading rules file: /usr/lib/udev/rules.d/13-dm-disk.rules
Reading rules file: /usr/lib/udev/rules.d/49-stlinkv1.rules
Reading rules file: /etc/udev/rules.d/49-stlinkv2-1.rules
Reading rules file: /etc/udev/rules.d/49-stlinkv2.rules
Reading rules file: /usr/lib/udev/rules.d/49-stlinkv3.rules
Reading rules file: /usr/lib/udev/rules.d/50-udev-default.rules
Reading rules file: /usr/lib/udev/rules.d/60-block.rules
Reading rules file: /usr/lib/udev/rules.d/60-cdrom_id.rules
Reading rules file: /usr/lib/udev/rules.d/60-drm.rules
Reading rules file: /usr/lib/udev/rules.d/60-evdev.rules
Reading rules file: /usr/lib/udev/rules.d/60-input-id.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-alsa.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-input.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage-tape.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-v4l.rules
Reading rules file: /usr/lib/udev/rules.d/60-rfkill.rules
Reading rules file: /usr/lib/udev/rules.d/60-sensor.rules
Reading rules file: /usr/lib/udev/rules.d/60-serial.rules
Reading rules file: /usr/lib/udev/rules.d/63-md-raid-arrays.rules
Reading rules file: /usr/lib/udev/rules.d/64-btrfs.rules
Reading rules file: /usr/lib/udev/rules.d/64-md-raid-assembly.rules
Reading rules file: /usr/lib/udev/rules.d/65-kvm.rules
Reading rules file: /usr/lib/udev/rules.d/65-libwacom.rules
Reading rules file: /usr/lib/udev/rules.d/69-cd-sensors.rules
Reading rules file: /usr/lib/udev/rules.d/69-dm-lvm-metad.rules
Reading rules file: /usr/lib/udev/rules.d/69-md-clustered-confirm-device.rules
Reading rules file: /usr/lib/udev/rules.d/69-yubikey.rules
Reading rules file: /usr/lib/udev/rules.d/70-infrared.rules
Reading rules file: /usr/lib/udev/rules.d/70-joystick.rules
Reading rules file: /usr/lib/udev/rules.d/70-mouse.rules
Reading rules file: /usr/lib/udev/rules.d/70-power-switch.rules
Reading rules file: /usr/lib/udev/rules.d/70-touchpad.rules
Reading rules file: /usr/lib/udev/rules.d/70-uaccess.rules
Reading rules file: /usr/lib/udev/rules.d/71-seat.rules
Reading rules file: /usr/lib/udev/rules.d/73-seat-late.rules
Reading rules file: /usr/lib/udev/rules.d/75-net-description.rules
Reading rules file: /usr/lib/udev/rules.d/75-probe_mtd.rules
Reading rules file: /usr/lib/udev/rules.d/78-sound-card.rules
Reading rules file: /usr/lib/udev/rules.d/80-docker.rules
Reading rules file: /usr/lib/udev/rules.d/80-drivers.rules
Reading rules file: /usr/lib/udev/rules.d/80-libinput-device-groups.rules
Reading rules file: /usr/lib/udev/rules.d/80-net-setup-link.rules
Reading rules file: /usr/lib/udev/rules.d/85-tlp.rules
Reading rules file: /usr/lib/udev/rules.d/89-alsa-ucm.rules
Reading rules file: /usr/lib/udev/rules.d/90-alsa-restore.rules
Reading rules file: /usr/lib/udev/rules.d/90-backlight.rules
Reading rules file: /usr/lib/udev/rules.d/90-libinput-fuzz-override.rules
Reading rules file: /usr/lib/udev/rules.d/90-pulseaudio.rules
Reading rules file: /usr/lib/udev/rules.d/90-vconsole.rules
Reading rules file: /usr/lib/udev/rules.d/95-cd-devices.rules
Reading rules file: /usr/lib/udev/rules.d/95-dm-notify.rules
Reading rules file: /usr/lib/udev/rules.d/96-e2scrub.rules
Reading rules file: /usr/lib/udev/rules.d/99-bumblebee-nvidia-dev.rules
Reading rules file: /usr/lib/udev/rules.d/99-fuse.rules
Reading rules file: /usr/lib/udev/rules.d/99-jlink.rules
/usr/lib/udev/rules.d/99-jlink.rules:356 Unknown group 'dialout', ignoring
/usr/lib/udev/rules.d/99-jlink.rules:357 Unknown group 'dialout', ignoring
/usr/lib/udev/rules.d/99-jlink.rules:358 Unknown group 'dialout', ignoring
Reading rules file: /etc/udev/rules.d/99-lowbat.rules
Reading rules file: /usr/lib/udev/rules.d/99-systemd.rules
DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:1b/PNP0C09:00/PNP0C0A:00/power_supply/BAT0
POWER_SUPPLY_NAME=BAT0
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_TECHNOLOGY=Li-poly
POWER_SUPPLY_CYCLE_COUNT=40
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=15360000
POWER_SUPPLY_VOLTAGE_NOW=15684000
POWER_SUPPLY_POWER_NOW=10476000
POWER_SUPPLY_ENERGY_FULL_DESIGN=80400000
POWER_SUPPLY_ENERGY_FULL=73880000
POWER_SUPPLY_ENERGY_NOW=48080000
POWER_SUPPLY_CAPACITY=65
POWER_SUPPLY_CAPACITY_LEVEL=Low
POWER_SUPPLY_MODEL_NAME=01AY969
POWER_SUPPLY_MANUFACTURER=Celxpert
POWER_SUPPLY_SERIAL_NUMBER=  675
ACTION=add
SUBSYSTEM=power_supply
USEC_INITIALIZED=7641133895
Unload module index
Unloaded link configuration context.

答案1

容量匹配未超过 9%
您正在将字符串与 0 到 9(含)之间的任何字符或字符 9 进行匹配。您需要指定比较字符串以"[1-9][0-9]"匹配 10 到 99 之间的任何字符。如果您还想匹配 10 以下的字符,您可以"[0-9]"在此处包含另一条规则。或者,您可以从脚本中或直接在 RUN 脚本中检查阈值,PROGRAN在脚本中,您可以使用数字比较或正则表达式以及更复杂的决策树。

相关内容