9

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
Yotam
  • 2,694
  • 6
  • 29
  • 32

5 Answers5

4

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"
done

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.

Arcege
  • 22,536
1

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

    MSSG="/tmp/mssg-file-${RANDOM}" 
    echo -e " MESSAGE \n ==========\n Done with task, YEY. " > ${MSSG}
    xmessage -center -file ${MSSG} -display :0.0 
    [[ -s ${MSSG} ]] && rm -f ${MSSG}
Mike Q
  • 159
1

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.

1

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.

jw013
  • 51,212
1

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
done

and my crontab looks like

55 09,13 * * * pkill -USR1 forward-notify
55 11 * * * pkill -USR2 forward-notify