引用这些链接没有成功。其中大多数似乎是特定于 ubuntu、gnome 和 cron 语法的。
- https://askubuntu.com/questions/298608/notify-send-doesnt-work-from-crontab/346580#346580
- https://stackoverflow.com/questions/16519673/cron-with-notify-send
- 当 Linux Mint 中的电池即将耗尽时,如何才能收到警报?
尝试导出显示、dbus、完整命令路径。
使用 dwm 运行 Arch Linux。使用 fcron 和 fcrontab -e。
定时任务表
@ 5s /usr/local/shsc/battlow
当我显式调用脚本时,它会按预期工作(/usr/shsc/battlow
)。
脚本
#!/bin/sh
export DISPLAY=:0
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if [ $battery_level -le 100 ]; then
notify-send "Battery low" "Battery level is ${battery_level}%!"
fi
尝试过使用和不使用export DISPLAY=:0
.
验证显示
$ echo $DISPLAY
:0
# systemctl status fcron
● fcron.service - fcron periodical command scheduler
Loaded: loaded (/usr/lib/systemd/system/fcron.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2021-05-18 08:36:13 MST; 12min ago
Main PID: 8778 (fcron)
Tasks: 1 (limit: 18916)
Memory: 2.3M
CPU: 3.182s
CGroup: /system.slice/fcron.service
└─8778 /usr/bin/fcron -b
May 18 08:48:08 bar fcron[11367]: Job '/usr/local/shsc/battlow' started for user foo (pid 11372)
May 18 08:48:08 bar fcron[11367]: Job '/usr/local/shsc/battlow' completed
May 18 08:48:13 bar fcron[11383]: Job '/usr/local/shsc/battlow' started for user foo (pid 11388)
May 18 08:48:13 bar fcron[11383]: Job '/usr/local/shsc/battlow' completed