#! /bin/bash
# Read battery percentage value
OUT=`upower -i /org/freedesktop/UPower/devices/battery_BAT1 | grep percentage`
# Select only the int value
IFS=':' read -ra P <<< "$OUT"
PERCENTAGE="%"
BATTERY_VALUE=${P[1]%$PERCENTAGE}
# Send a notification if battery level is under 15% and not on AC power
if ! on_ac_power; then
if (($BATTERY_VALUE < "15")); then
eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
notify-send -u critical -i "battery-caution" "Battery low! You should plug in your laptop!"
/usr/bin/aplay /usr/share/sounds/desktop-logout.oga
fi
fi
# Send a notification if battery level is equal to or over 90% and is on AC power
if on_ac_power; then
if (($BATTERY_VALUE >= "90")); then
eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
notify-send -u critical -i "battery-full-charging" "Battery charged! You should unplug your laptop!"
/usr/bin/aplay /usr/share/sounds/desktop-logout.oga
fi
fi
如果我手动执行脚本,声音和脚本工作正常。我收到来自notify-send的通知,但它没有播放来自crontab的声音。Cron日志显示:
Connection failure: Connection refused
pa_context_connect() failed: Connection refused.
我尝试过该命令paplay
并指向 paplay 的绝对路径但/usr/bin/paplay
无济于事。