Make notify-send work from user cron file in ubuntu

In ubuntu, you can use notify-send to show notificactions. But if you try to show notification from crontab, it will fail: notify-send requires proper values in $DBUS_SESSION_BUS_ADDRESS and $DISPLAY. To override this disappointing limitation, you can grep this values from some known process of yours. Here is example that works for XFCE, Gnome, Unity, Cainnamon and KDE:


[ "$#" -lt 1 ] && echo "Usage: $0 program options" && exit 1


env_reference_process=$( pgrep -u "$user" xfce4-session || pgrep -u "$user" ciannamon-session || pgrep -u "$user" gnome-session || pgrep -u "$user" gnome-shell || pgrep -u "$user" kdeinit )

export DBUS_SESSION_BUS_ADDRESS=$(cat /proc/"$env_reference_process"/environ | grep -z ^DBUS_SESSION_BUS_ADDRESS= | sed 's/DBUS_SESSION_BUS_ADDRESS=//')
export DISPLAY=$(cat /proc/"$env_reference_process"/environ | grep -z ^DISPLAY= | sed 's/DISPLAY=//')
"$program" "$@"


*/45 * * * * /usr/local/bin/ notify-send --expire-time=30000 --icon=dialog-information "GET UP AND EXERCISE"

License: Creative Commons Attribution-ShareAlike License Share: share on reddit share on facebook share on twitter