在新版本的gnome-ubuntu系統中,發現crontab 中的 notify-send 不能顯示通知(某些版本的ubuntu中是可以顯示的,如Mint),但命令確實執行了,因為其中的無UI命令都執行了。當然后面Google到了具體的解決辦法,記錄如下:
寫一個代碼引用otify-send :
#!/bin/bash
# showtips
source /home/work/.bashrc
pid=$(pgrep -u work gnome-session | head -n 1)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )
export DBUS_SESSION_BUS_ADDRESS=$dbus
export HOME=/home/work
export DISPLAY=:0
/usr/bin/notify-send $1 $2 $3 $4
注意變更其中的目錄/home/work/為你實際的目錄路徑,保存為/usr/bin/showtips,賦于showtips運行權限:
sudo chmod +x /usr/bin/showtips
然后在crontab中使用showtips代替 notify-send
56-59 16 * * * DISPLAY=:0 /usr/bin/showtips -i /home/work/tips.png "提醒" "準備下班!"
經測試,通知完美顯示。