I am toying around with notify-send and cron (on an Arch machine) and I can't figure out a way to combine them:

I tried the solution given here and here, but neither worked. How can I use them?

EDIT: I have set the DISPLAY in the crontab and it still didn't work. I tried the same in Ubuntu and there things are working fine. Here is my cron line:

*/1 * * * * DISPLAY=:0.0 /usr/bin/notify-send "hellp" || echo "er" > .er
  • 1
    In what way did they not work? Did you remember to set DISPLAY? – Michael Mrozek Sep 8 '11 at 20:53
  • What are the error messages (they'll be mailed to you by local email, make sure you have an MTA installed and check /var/mail/yotam or /var/spool/mail/yotam)? You may need to set XAUTHORITY in addition to DISPLAY, see this answer. – Gilles 'SO- stop being evil' Sep 8 '11 at 22:13
  • Does an "Authentication failure" message appear in /var/log/cron? If it appears, see if you have an /etc/pam.d/crond file on Ubuntu. If you have, steal it, otherwise see the cron job not running because of pam? thread in the openSUSE forum. – manatwork Sep 9 '11 at 7:11

About the only thing that I can suggest is to create a named pipe and have cron write to the pipe and have a little script started by the session manager that reads from the pipe and calls notify-send:

while read line < /tmp/.cron2notify.s  # pipe name in /tmp
do notify-send "Cron message" "$line"

Then in the crontab, have the program write to /tmp/.cron2notify.s.

Haven't tested this, but should give you a starting point to work from.

| improve this answer | |

I think that notify-send relies on on D-Bus on most systems, and the most common method of contacting dbus is via the $DBUS_SESSION_BUS_ADDRESS variable. However hard-coding this in crontab is probably not very workable since the bus address changes every time you start a new instance of dbus which usually happens whenever you log in or reboot. Arcege's solution sounds like a good workaround.

| improve this answer | |

I would really recommend using a wrapper script. To emulate cron calling notify-send, I sshed into my own system with "ssh localhost". Calling '/usr/bin/notify-send "foo"' Didn't work, and I had no luck when adding DISPLAY=:0.0 to the line. Here is what did work:

DBUS_SESSION_BUS_ADDRESS="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/`pgrep -u username cinnamon|head -1`/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//')" /usr/bin/notify-send "foo"

That is VERY messy, to say the least. This is cleaner.

#! /bin/bash

DBUS=$(pgrep -ou $1 cinnamon)
DBUS="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$DBUS/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//')"

DBUS_SESSION_BUS_ADDRESS="$DBUS" /usr/bin/notify-send $2

And in cron:

*/1 * * * * /path/to/notify.sh user "help"

Please note that the "cinnamon" bit is there because that the window manager I use. You will likely have to replace it with nautilus, or whatever else you have as a window manager. I also have a user specified as the first argument, because it makes it more modular later, if you want this to work regardless of who is logged in.

| improve this answer | |
  • Worked flawlessly :) Thank you very much! – AkiRoss Sep 28 '14 at 10:24

I tried to use Arcege's answer, but could not make it work: the while loop terminates after the first line is written to the named pipe. What worked for me was having a similar script with an infinite loop, in which I trap signals sent from cron and then call notify-send. The following is the program "notify-forward":

#! /bin/bash
function coffee () {
   notify-send "foo" "bar"
function lunch () {
   notify-send "bar" "foo" 
while true; do
  sleep 10
  trap coffee SIGUSR1
  trap lunch SIGUSR2

and my crontab looks like

55 09,13 * * * pkill -USR1 forward-notify
55 11 * * * pkill -USR2 forward-notify
| improve this answer | |

Alternative: You could wrestle with all of what you guys are doing or just use the xmessage option:

    echo -e " MESSAGE \n ==========\n Done with task, YEY. " > ${MSSG}
    xmessage -center -file ${MSSG} -display :0.0 
    [[ -s ${MSSG} ]] && rm -f ${MSSG}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.