51

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?

toro2k
  • 19,020
  • 7
  • 64
  • 71
Ravi
  • 1,574
  • 2
  • 16
  • 28

14 Answers14

106

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.

Mr. Goferito
  • 6,391
  • 4
  • 26
  • 27
25

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"
Community
  • 1
  • 1
Ravi
  • 1,574
  • 2
  • 16
  • 28
  • 8
    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. – Ruben Jul 15 '13 at 23:56
  • Thanks, that helped me too (I didn't need to use "sudo") – Erel Segal-Halevi Feb 15 '14 at 17:05
  • 3
    Me too. I could reduce this command to `* * * * * export DISPLAY=:0.0 && notify-send Hey "How are you"` – TomTom Apr 09 '15 at 09:47
  • 4
    It can be further reduced to `* * * * * DISPLAY=:0.0 notify-send Hey "How are you"` – tripleee Mar 27 '17 at 21:20
  • @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` – Michael Campbell Oct 18 '19 at 18:55
  • This doesn't work on Ubuntu 21 but this https://stackoverflow.com/a/53598510/10633734 answer by Mr. Goferito does – Daniel Titkov Jul 30 '21 at 22:55
7

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

Community
  • 1
  • 1
nmax
  • 981
  • 1
  • 11
  • 19
  • 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. – lash Sep 08 '20 at 16:26
6

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

Fangxing
  • 5,716
  • 2
  • 49
  • 53
  • 1
    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)"` – urban_raccoons May 15 '18 at 10:43
6

add DBUS_SESSION_BUS_ADDRESS

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

许立山
  • 61
  • 1
  • 1
3

Work for me on fedora 22:

Put this line in the .sh script before notify-send get called:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)"
uol3c
  • 559
  • 5
  • 9
2

On recent Ubuntu versions, the following should work.

#notify_me.sh, can be placed e.g. in your home directory

#!/bin/bash
eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
# the actual notification
DISPLAY=:0 notify-send "Notify me!"

Then you add a line to your user's cronjobs via crontab as usual.

imrek
  • 2,930
  • 3
  • 20
  • 36
1

Simple and reduced answer:

01 * * * * export DISPLAY=:0.0 && notify-send Hey "How are you"

If you need Xauthority permission, here's a generalizable form using the $LOGNAME variable

01 * * * * export DISPLAY=:0.0 && && export XAUTHORITY=/home/$LOGNAME/.Xauthority notify-send Hey "How are you"

As pointed out by @tripleee, there's no real need for sudo here

Weevils
  • 312
  • 3
  • 9
0

I created a /usr/bin script that uses the DISPLAY-:0.0 technique http://pastebin.com/h11p2HtN

It doesn't take XAUTHORITY into account. I'll have to investigate that further.

Lance Caraccioli
  • 1,421
  • 13
  • 14
0

Similar to nmax above, I also solved the issue by setting DBUS_SESSION_BUS_ADDRESS environment variable. However, I am using a combination of Linux Mint 19 (xfce4) and XMonad and for some reason, I don't have the process xfce4-session running. Instead, I found that xfce4-terminal is (usually) running, which resulted in the following line in the beginning of my script:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-terminal)/environ)"

This resolved the issue for me.

0

If you are having issue that your notify-send script is working fine locally but it is not working with cron jobs then use this command. and replace "saurav" with your username.

sudo -u saurav DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Your message goes here"
0

In my case, the issue was with using root user with notify-send. I realized this when I saw that sudo notify-send didn't work on the terminal but just notify-send (i.e. with current user) did. So instead of editing the usual /etc/crontab which is used by root, I had a custom cron script created for myusername. Please check my whole answer here.

abcoep
  • 577
  • 9
  • 14
-1

May be you can try:

* * * * * env DISPLAY=:0.0 sudo -u ravi /usr/bin/notify-send Hey "How are you"

Leslie Zhu
  • 139
  • 2
  • 7
  • Downvote: The cron job will already be running as `ravi` so the `sudo` is not adding anything useful here. – tripleee Mar 27 '17 at 21:21
-3

Try this when you call notify-send in your script:

echo "PASSWORD" | sudo -u USER notify-send "your alert message"