Cron with notify-send

18,001

Solution 1

I found the answer:

$ crontab -l
# m h  dom mon dow   command
  * *   *   *   *    export DISPLAY=:0.0 && export XAUTHORITY=/home/ravi/.Xauthority && sudo -u ravi /usr/bin/notify-send Hey "How are you"

Solution 2

I use i3 on Ubuntu 18.04. My way to solve this is:

* * * * * XDG_RUNTIME_DIR=/run/user/$(id -u) notify-send Hey "this is dog!"

Edit 2020: I still use it on Ubuntu 20.04.

Solution 3

In Ubuntu 14.04 exporting the display did not work for me. Below is a cron script I'm using to shutdown a virtual machine when a laptop's battery state becomes too low. The line setting DBUS_SESSION_BUS_ADDRESS is the modification that finally got the warnings working correctly.

#!/bin/bash

# if virtual machine is running, monitor power consumption
if pgrep -x vmware-vmx; then

  bat_path="/sys/class/power_supply/BAT0/"

  if [ -e "$bat_path" ]; then

    bat_status=$(cat $bat_path/status)

    if [ "$bat_status" == "Discharging" ]; then

      bat_current=$(cat $bat_path/capacity)

      # halt vm if critical; notify if low
      if [ "$bat_current" -lt 10 ]; then

        /path/to/vm/shutdown/script
        echo "$( date +%Y.%m.%d_%T )" >> "/home/user/Desktop/VM Halt Low Battery"

        elif [ "$bat_current" -lt 15 ]; then
        eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
        notify-send -i "/usr/share/icons/ubuntu-mono-light/status/24/battery-caution.svg"  "Virtual machine will halt when battery falls below 10% charge."

      fi

    fi

  fi

fi

exit 0

The relevant line is here:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";

I found the solution here: https://askubuntu.com/a/346580/255814

Solution 4

Only this works for me (Xubuntu)

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-session)/environ)"; notify-send  "hello world" 

If you are in gnome enviroment, you need change xfce4-session to gnome-session

refer: https://askubuntu.com/questions/298608/notify-send-doesnt-work-from-crontab

Solution 5

add DBUS_SESSION_BUS_ADDRESS

* * * * * DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /usr/bin/notify-send 'helloworld..' 'msg...'

Share:
18,001
Ravi
Author by

Ravi

Updated on June 03, 2022

Comments

  • Ravi
    Ravi almost 2 years

    I need to show a notification from a cron job. My crontab is something like:

    $ crontab -l
    # m h  dom mon dow   command
      * *   *   *   *    Display=:0.0 /usr/bin/notify-send Hey "How are you"
    

    I checked /var/log/syslog and the command is actually executed every minute but it doesn't pop up the notification. Can anybody help me understand why?

  • Ruben
    Ruben almost 11 years
    Just adding "export DISPLAY=:0.0" did the trick for me. That said, the cronjob is running under my user account. XAUTHORITY is probably need when run from a different account than the one where the popup needs to appear. Thanks.
  • Erel Segal-Halevi
    Erel Segal-Halevi about 10 years
    Thanks, that helped me too (I didn't need to use "sudo")
  • TomTom
    TomTom about 9 years
    Me too. I could reduce this command to * * * * * export DISPLAY=:0.0 && notify-send Hey "How are you"
  • tripleee
    tripleee about 7 years
    It can be further reduced to * * * * * DISPLAY=:0.0 notify-send Hey "How are you"
  • tripleee
    tripleee about 7 years
    Downvote: The cron job will already be running as ravi so the sudo is not adding anything useful here.
  • urban_raccoons
    urban_raccoons almost 6 years
    As of ubuntu 17.10 I believe you need to be looking for gdm-x-session. Here's the line I used: eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gdm-x-session)/environ)"
  • Michael Campbell
    Michael Campbell over 4 years
    @DrunkenMaster I just tried this on 18.04; worked for me. The crontab is my own (not root's) * * * * * export DISPLAY=:0.0 && notify-send -t 10000 Hey Hey
  • Kyle
    Kyle almost 4 years
    Can confirm this works on Ubuntu 18.04 where the accepted answer did not.
  • user81371
    user81371 almost 4 years
    Can confirm this works on Ubuntu 20.04 when the confirmed answer did not.
  • lash
    lash over 3 years
    I use archlinux, which uses a different process for the gnome-session, but still with the string gnome-session as a long flag. Thus needed to add -f to the pgrep invocation.
  • loop
    loop over 3 years
    This should be the confirmed answer now. I subconsciously went straight to the accepted answer which didn't work, and therefore initially missed the working solution.
  • Daniel Titkov
    Daniel Titkov over 2 years
    This doesn't work on Ubuntu 21 but this stackoverflow.com/a/53598510/10633734 answer by Mr. Goferito does
  • eiro
    eiro over 2 years