Sending Desktop Notification from Cron

Usually when one wants to keep track of one’s cron jobs, one tells the cron daemon to email the output of the commands. While this is probably the best solution for servers, on desktop machines is problematic. Many ISPs block outgoing traffic on port 25 (SMTP), and if you want to send the emails via external SMTP server (such as GMail) this requires you to store authentication details in plain text. A better solution for the desktop would be to harness the desktop notifications available in Ubuntu.

There is a useful tool called notify-send which is able to send desktop notifications directly from the command line. However, there are few caveats:

  • notify-send expects its input on the command line, it can’t read from stdin.
  • If you run from cron you must tell it which display to use.

The first issue can be worked around by using cat to pick up the input. The second issue is handled by adding a DISPLAY environment variable to the crontab. So your crontab will look something like this:

DISPLAY=:0
10 1 * * sun some_cool_command | notify-send "Backup Documents" "$(cat)"

The first argument to notify-send is the title of the notification. The second is the actual text to appear in it, in our case it’s whatever comes in the stdin. If you want to store the output in a log file as well as displaying it in a desktop notification, you can use tee, which basically saves its input to a given file and also pipes it again to stdout.

DISPLAY=:0
10 1 * * sun some_cool_command | tee -a ~/some_log.log | notify-send "Backup Documents" "$(cat)"

6 thoughts on “Sending Desktop Notification from Cron

  1. Unonymous

    GNOME Shell on Ubuntu?
    May I ask why?
    Aren’t you suffering from compatibility problems too often?

  2. Ehud

    There is the old UNIX xmessage, why don’t you use it ?
    what is better with notify-send ?
    the xmessage has full documentation. I think it works on all UNIX systems with X system (even on MacOS and Cygwin). I’m using for a very long time (since about 1997)
    do man xmessage or Check: http://linux.die.net/man/1/xmessage

  3. Guy Post author

    @Unonymous: I’ve gave Unity a try and used it for couple of months, however when I tried GNOME Shell, it felt better for me and it seems that I’m more productive with it. Also, I had some irritating things with Unity regarding my dual-screen setup (like the thing where the cursor sticks to the edge of the screen, which can’t be adjusted…). I have some compatibility issues regarding themes, which sadly manifest itself primarly in Ubuntu Software Center, but nothing show-stopping.

    In the end of the day, I think both are good, and I’ve enjoyed both of them, however GNOME was slightly better.

    @Ehud: I didn’t know xmessage before you mentioned it, thanks! From a quick look it seems that notify-send has better integration with the notification system per the proposed Desktop Notifications Specification, while xmessage lacks it. xmessage looks useful, and I’ll surely take a more thorough look into it.

  4. Unonymous

    Are you using Ubuntu GNOME Shell Remix,
    or a plain ubuntu with gnome-shell installed?
    Any comments on any of the alternatives?

    Thanks. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>