<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Enodev.fr / Christophe's log (Posts about D-Bus)</title><link>https://www.enodev.fr/</link><description></description><atom:link href="https://www.enodev.fr/categories/d-bus.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Sun, 10 Mar 2024 09:58:50 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Send desktop notifications with cron</title><link>https://www.enodev.fr/posts/send-desktop-notifications-with-cron.html</link><dc:creator>Christophe Vu-Brugier</dc:creator><description>&lt;p&gt;At work, I tend to check my mailbox too often. That's unnecessary. As
an experiment, I have decided to check it once every hour. Since I
don't want to spend "brain cycles" remembering when I should check my
mailbox, I have added a cron task to display a notification every
hour.&lt;/p&gt;
&lt;p&gt;Desktop notifications on Linux can be displayed with a command-line
utility named &lt;code&gt;notify-send&lt;/code&gt;:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;notify-send&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hi &lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;USER&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"it's time to check your email."&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;--icon&lt;span class="o"&gt;=&lt;/span&gt;dialog-information
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The notification is displayed by the
&lt;a href="https://wiki.archlinux.org/index.php/Desktop_notifications#Notification_servers"&gt;notification server&lt;/a&gt;
which is either part of your desktop environment or standalone. I
personally use &lt;a href="https://dunst-project.org/"&gt;dunst&lt;/a&gt; because my window
manager, &lt;a href="https://i3wm.org"&gt;i3&lt;/a&gt;, has no built-in notification server.&lt;/p&gt;
&lt;p&gt;However, when &lt;code&gt;notify-send&lt;/code&gt; is invoked by cron, nothing is
displayed. The script invoked by cron cannot communicate with the
desktop environment because the &lt;code&gt;DBUS_SESSION_BUS_ADDRESS&lt;/code&gt; variable is
not set.&lt;/p&gt;
&lt;p&gt;As a consequence, you have to retrieve the value of the
&lt;code&gt;DBUS_SESSION_BUS_ADDRESS&lt;/code&gt; for your session. You can do that by
parsing the &lt;code&gt;/proc/$PID/environ&lt;/code&gt; pseudo file of your window manager
(&lt;a href="https://i3wm.org"&gt;i3&lt;/a&gt; in my case):&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;grep&lt;span class="w"&gt; &lt;/span&gt;-z&lt;span class="w"&gt; &lt;/span&gt;DBUS_SESSION_BUS_ADDRESS&lt;span class="w"&gt; &lt;/span&gt;/proc/&lt;span class="k"&gt;$(&lt;/span&gt;pidof&lt;span class="w"&gt; &lt;/span&gt;i3&lt;span class="k"&gt;)&lt;/span&gt;/environ&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;cut&lt;span class="w"&gt; &lt;/span&gt;-d&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-f2-
&lt;span class="go"&gt;unix:abstract=/tmp/dbus-ajRnZ5v7g9,guid=f8761603e1845a282e104e1a5515bcec&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I wrote the following shell script named &lt;code&gt;remind-email&lt;/code&gt; to grab the
bus address of the D-Bus session and display a desktop notification:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="ch"&gt;#!/bin/sh&lt;/span&gt;

&lt;span class="nb"&gt;export&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;DBUS_SESSION_BUS_ADDRESS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;grep&lt;span class="w"&gt; &lt;/span&gt;-z&lt;span class="w"&gt; &lt;/span&gt;DBUS_SESSION_BUS_ADDRESS&lt;span class="w"&gt; &lt;/span&gt;/proc/&lt;span class="k"&gt;$(&lt;/span&gt;pidof&lt;span class="w"&gt; &lt;/span&gt;i3&lt;span class="k"&gt;)&lt;/span&gt;/environ&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;cut&lt;span class="w"&gt; &lt;/span&gt;-d&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-f2-&lt;span class="k"&gt;)&lt;/span&gt;
notify-send&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hi &lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;LOGNAME&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"it's time to check your email."&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;--icon&lt;span class="o"&gt;=&lt;/span&gt;dialog-information
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The script is invoked every hour by the following cron entry (use
&lt;code&gt;crontab -e&lt;/code&gt; to edit your cron tables):&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="c1"&gt;remind-email&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</description><category>cron</category><category>D-Bus</category><category>notify-send</category><guid>https://www.enodev.fr/posts/send-desktop-notifications-with-cron.html</guid><pubDate>Fri, 27 Mar 2015 18:00:00 GMT</pubDate></item></channel></rss>