我想使用计时器在systemd中编写cron作业替换,以检查我的电池电量,并使用i3-nagbar在电池电量低于某个特定水平时给我警告。我查看了stackoverflow中的其他问题,但都通过设置我完成的display和xauthority变量解决了所有问题。
服务文件:
[Unit]
Description=Low battery warning
[Service]
Environment="DISPLAY=:0" "XAUTHORITY=/home/leon/.Xauthority"
Type=oneshot
ExecStart=/bin/bash /home/leon/.miscellaneous/battery.sh
[Install]
WantedBy=multi-user.target
计时器文件:
[Unit]
Description=Low battery timer
[Timer]
OnUnitActiveSec=5min
OnBootSec=0min
[Install]
WantedBy=timers.target
脚本:
#!/bin/bash
source /sys/class/power_supply/BAT0/uevent
echo $POWER_SUPPLY_STATUS
echo $POWER_SUPPLY_CAPACITY
if [ "$POWER_SUPPLY_STATUS" == "Discharging" ]; then
if [ $POWER_SUPPLY_CAPACITY -lt 30 ]; then
echo "low battery"
/bin/i3-nagbar -t warning -m "Warning: Low Battery" &
elif [ $POWER_SUPPLY_CAPACITY -lt 10 ]; then
systemctl suspend
fi
fi
systemctl状态batt_warn.service:
Apr 20 21:32:12 krypton systemd[1]: Starting Low battery warning...
Apr 20 21:32:12 krypton systemd[1]: batt_warn.service: Succeeded.
Apr 20 21:32:12 krypton systemd[1]: Started Low battery warning.
Apr 20 21:32:12 krypton bash[9770]: Discharging
Apr 20 21:32:12 krypton bash[9770]: 27
Apr 20 21:32:12 krypton bash[9770]: low battery
使用i3-nagbar电池电量低时,应该给我一个警告。目前该脚本正在定期执行,但nag-bar似乎没有出现。