11

I really read many tutorials, but crontab doesn't start notify send.

What I've tried:

  1. etc/crontab

    added at the end:

    15 * * * * notify-send 'crontab' 'test'
    
  2. with crontab interface:

    crontab -e
    

    added at the end:

    15 * * * * notify-send 'crontab' 'test'
    
Jacob Vlijm
  • 83,767
Vitor Abella
  • 7,537
  • 3
    Try to run .sh script by editing eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)"; – d a i s y Oct 08 '16 at 07:23
  • @Lnux , Where do I place this line? On .sh file before the notify-send 'crontab' 'test' ? – Vitor Abella Oct 08 '16 at 07:44
  • yes in your bash script before notify-send – d a i s y Oct 08 '16 at 07:51
  • on 16.04 this one worked
    `*/1 * * * * eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";/usr/bin/notify-send -i  appointment  -c "im" "Keep Working"`
    
    – Midhun KM Jul 08 '18 at 05:56
  • * * * * * export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ) && DISPLAY=:0 notify-send "Foo bar" – Saeed Falsafin Dec 21 '18 at 15:03

2 Answers2

13

As suggested by @Lnux:

Create a .sh, for example test.sh:

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

#Code:
DISPLAY=:0 notify-send "Test"

Then set up crontab:

crontab -e

And at the bottom, add:

* * * * * /home/myUser/test.sh

Obs.: you can place your .sh file in another location and don't forget to allow executing it.

Vitor Abella
  • 7,537
  • In case it helps anyone else, I was able to replace the provided eval ... with: eval "export | egrep 'DBUS_SESSION_BUS_ADDRESS|DISPLAY'". – pdoherty926 Nov 09 '17 at 15:10
  • I got a large output which I don't understand, so I replaced the eval ... with the line suggested by @pdoherty926 and I got this output. declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1002/bus" declare -x DISPLAY=":1" . So I changed the next line to DISPLAY=:1 notify-send "Test". Still no notification with crontab – kksagar Jul 29 '21 at 09:12
6

Crontab is running in the background and without specified graphical output it will show nothing. So you should spicify, which output would you like to use. To achieve that, put export DISPLAY=:0 && before you command, e.g.

15 * * * * export DISPLAY=:0 && notify-send 'I am alive!'

  • 'DISPLAY=:' or 'DISPLAY=' – Vitor Abella Oct 08 '16 at 07:02
  • DISPLAY=:0 , the colon is neccessary. – Michal Polovka Oct 08 '16 at 07:08
  • I copied and pasted your code using crontab -e using user and root and nothing happens... – Vitor Abella Oct 08 '16 at 07:14
  • well that is unexpected, because I did the same (except I changed 15 minutes to *) and it works. Try to change the DISPLAY variable to 1 or 2, I have seen it help sometimes, although I don't fully understand why. – Michal Polovka Oct 08 '16 at 07:20
  • Do you have to reboot to start? – Vitor Abella Oct 08 '16 at 07:25
  • No, it works with next launch-time – Michal Polovka Oct 08 '16 at 08:15
  • @Michael I think your solution didn't work because I use multiple desktop enfironment. – Vitor Abella Oct 08 '16 at 22:59
  • I also use several desktops, but it's possible, that the numbering depends on graphical card/driver. I have similar problem with another machine to which I was connected via ssh and was unable to start notify-send with DISPLAY=:0. – Michal Polovka Oct 09 '16 at 09:35
  • Try echo $DISPLAY in an xshell, to see to what it is set on your system. However, on Xubuntu-20.04 it didn't work, but export XDG_RUNTIME_DIR=/run/user/1000; notify-send -u critical TEST displaytest worked, while I'm the only desktop user, and my UID is 1000, which is the default for all xyUbuntus so far. You may as well evaluate the UID at runtime. – user unknown Apr 08 '21 at 13:47