首页 > 解决方案 > i3-nagbar 未从 systemd 服务执行(已设置显示和 xauthority)

问题描述

我想在 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 似乎没有出现。

标签: systemd

解决方案


推荐阅读