2

In my Ubuntu 16.04 , 64 bit OS, I tried the following

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

And

* * * * * export DISPLAY=:0.0 && /usr/bin/notify-send Hey "How are you"

But, unfortunately, it is not working.

I found in other threads that the above command works.

How to run it on my machine?

The command notify-send Hey "How are you" works on the terminal.

Also the command * * * * * echo "trying to notify at $(date)" >> /home/user/Desktop/test.txt works fine from crontab file

The output of $ echo $DISPLAY is :0

also

$ who -u
cosmicraga   tty7         2016-11-07 06:45 06:12        2524 (:0)
cosmicraga   pts/1        2016-11-07 12:54   .          6333 (:0)
cosmicraga   pts/17       2016-11-07 12:50 00:02        6333 (:0)
Zanna
  • 69,223
  • 56
  • 216
  • 327
cosmicraga
  • 599
  • 3
  • 7
  • 19
  • I assume you are trying send message to remote machine (ssh) ? – Liso Nov 07 '16 at 07:21
  • no I am trying to have desktop notification on the current machine – cosmicraga Nov 07 '16 at 07:22
  • Your $DISPLAY variable isn't necessarily :0 . What is the output of `echo $DISPLAY`? (or `who -u`) – Jacob Vlijm Nov 07 '16 at 07:22
  • The output is `$ echo $DISPLAY :0 ` I have updated the question with this info. – cosmicraga Nov 07 '16 at 07:25
  • Ultimately it worked. I found the solution by @Vitor Abella in the following answer [The Solution](http://askubuntu.com/questions/834476/how-to-use-notify-send-with-crontab?rq=1) I wish Ubuntu makes it easier to implement. – cosmicraga Nov 08 '16 at 06:09

1 Answers1

3

Create notify.sh file in the home directory.

#!/usr/bin/env bash

username=$(/usr/bin/whoami)
pid=$(pgrep -u $username nautilus)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )
export DBUS_SESSION_BUS_ADDRESS=$dbus

/usr/bin/notify-send "How are you"

In the crontab :

* * * * *  DISPLAY=0:0 /bin/sh /home/YOURUSERNAME/notify.sh
Walk
  • 171
  • 6
  • I'm sorry but this solution does not works. I'm still not getting the notification – Gardezi Jul 10 '17 at 07:06
  • 1
    it worked like a charm. @Gardezi, create notify.sh file in your home directory with the first content supplied by @Sonu, then open crontab with `crontab -e` command in command line. than type the second content supplied by Sonu to the end of the file, use these shortcuts to save and exit: CTRL + O, ENTER, CTRL + X... That's it. but, please don't forgot to change the "netcoreps1" text in the second content supplied by @Sonu, with your current username.. for example: `* * * * * DISPLAY=0:0 /bin/sh /home/YOURUSERNAME/notify.sh`.. it should work at start of every minute. –  Nov 08 '17 at 00:28
  • 1
    @Interesting Knox , Thats correct. I have improved my answer as per ur comment. Thanks. :) – Walk Dec 05 '17 at 06:10
  • finally this worked for me :) – Sudip Bhandari May 21 '19 at 18:36