<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>le-Web.org &#187; cron</title>
	<atom:link href="http://www.le-web.org/tag/cron/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.le-web.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 04 Dec 2008 10:21:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PyGTK: How to display a systray icon from a cronjob</title>
		<link>http://www.le-web.org/2008/11/06/pygtk-how-to-display-a-systray-icon-from-a-cronjob/</link>
		<comments>http://www.le-web.org/2008/11/06/pygtk-how-to-display-a-systray-icon-from-a-cronjob/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 11:51:58 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python/PyGTK]]></category>
		<category><![CDATA[Tips, Tricks & Scripts]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[pygtk]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[systray]]></category>

		<guid isPermaLink="false">http://www.le-web.org/?p=55</guid>
		<description><![CDATA[It is nice to give some user feedback when someting happen in a background application. For example when a cronjob is running it would be nice to show a systray icon.


When the cron-job runs the DISPLAY environment variable is not defined so your gtk application can&#8217;t access to xserver. So before importing pygtk you should [...]]]></description>
			<content:encoded><![CDATA[<p>It is nice to give some user feedback when someting happen in a background application. For example when a cronjob is running it would be nice to show a systray icon.<br />
<span id="more-55"></span><br />
<!--adsense--><br />
When the cron-job runs the DISPLAY environment variable is not defined so your gtk application can&#8217;t access to xserver. So before importing pygtk you should check if DISPLAY is defined. If not just define it to default value &#8220;:0.0&#8243;.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#if DISPLAY is not set, then set it to default ':0.0'</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">getenv</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">'DISPLAY'</span>, <span style="color: #483d8b;">''</span> <span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
	<span style="color: #dc143c;">os</span>.<span style="color: black;">putenv</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">'DISPLAY'</span>, <span style="color: #483d8b;">':0.0'</span> <span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> pygtk
pygtk.<span style="color: black;">require</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2.0&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>But what happens if your gtk application really can&#8217;t connect to the xserver (xserver is not runnig, you are not logged in &#8230;). In this case your application should not try to use xserver related functions. For example when I try to use gtk.StatusIcon the application ends with a segmentation fault.</p>
<p>To check if your application can access to xserver just get the default display. If it is None then you can&#8217;t access it.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">display = gtk.<span style="color: black;">gdk</span>.<span style="color: black;">display_get_default</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> display <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
	...</pre></div></div>

<p><!--adsense--><br />
Now, putting all together I made this simple application: <a href="http://www.le-web.org/wp-content/uploads/2008/11/notify.py">notify.py</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>br /<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>b<span style="color: #66cc66;">&gt;</span>Warning<span style="color: #66cc66;">&lt;</span>/b<span style="color: #66cc66;">&gt;</span>:  include<span style="color: black;">&#40;</span>/home2/macadanc/public_html/le-web.<span style="color: black;">org</span>/wp-content/uploads/<span style="color: #ff4500;">2008</span>/<span style="color: #ff4500;">11</span>/notify.<span style="color: black;">py</span><span style="color: black;">&#41;</span> <span style="color: black;">&#91;</span><span style="color: #66cc66;">&lt;</span>a href=<span style="color: #483d8b;">'function.include'</span><span style="color: #66cc66;">&gt;</span>function.<span style="color: black;">include</span><span style="color: #66cc66;">&lt;</span>/a<span style="color: #66cc66;">&gt;</span><span style="color: black;">&#93;</span>: failed to <span style="color: #008000;">open</span> stream: No such <span style="color: #008000;">file</span> <span style="color: #ff7700;font-weight:bold;">or</span> directory <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>b<span style="color: #66cc66;">&gt;</span>/home2/macadanc/public_html/le-web.<span style="color: black;">org</span>/wp-content/plugins/include-it/plugin.<span style="color: black;">php</span><span style="color: #66cc66;">&lt;</span>/b<span style="color: #66cc66;">&gt;</span> on line <span style="color: #66cc66;">&lt;</span>b<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">86</span><span style="color: #66cc66;">&lt;</span>/b<span style="color: #66cc66;">&gt;&lt;</span>br /<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>br /<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>b<span style="color: #66cc66;">&gt;</span>Warning<span style="color: #66cc66;">&lt;</span>/b<span style="color: #66cc66;">&gt;</span>:  include<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#91;</span><span style="color: #66cc66;">&lt;</span>a href=<span style="color: #483d8b;">'function.include'</span><span style="color: #66cc66;">&gt;</span>function.<span style="color: black;">include</span><span style="color: #66cc66;">&lt;</span>/a<span style="color: #66cc66;">&gt;</span><span style="color: black;">&#93;</span>: Failed opening <span style="color: #483d8b;">'/home2/macadanc/public_html/le-web.org/wp-content/uploads/2008/11/notify.py'</span> <span style="color: #ff7700;font-weight:bold;">for</span> inclusion <span style="color: black;">&#40;</span>include_path=<span style="color: #483d8b;">'.:/usr/share/pear'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>b<span style="color: #66cc66;">&gt;</span>/home2/macadanc/public_html/le-web.<span style="color: black;">org</span>/wp-content/plugins/include-it/plugin.<span style="color: black;">php</span><span style="color: #66cc66;">&lt;</span>/b<span style="color: #66cc66;">&gt;</span> on line <span style="color: #66cc66;">&lt;</span>b<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">86</span><span style="color: #66cc66;">&lt;</span>/b<span style="color: #66cc66;">&gt;&lt;</span>br /<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p><!--adsense--><br />
Try it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python notify.py</pre></div></div>

<p>Now to try it from cron, you can add it in you crontab to be called every 10 minutes (use full path to notify.py):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span> crontab -l; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;0 * * * * python /FULLPATH/notify.py&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> crontab -
<span style="color: #7a0874; font-weight: bold;">&#40;</span> crontab -l; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;10 * * * * python /FULLPATH/notify.py&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> crontab -
<span style="color: #7a0874; font-weight: bold;">&#40;</span> crontab -l; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;20 * * * * python /FULLPATH/notify.py&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> crontab -
<span style="color: #7a0874; font-weight: bold;">&#40;</span> crontab -l; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;30 * * * * python /FULLPATH/notify.py&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> crontab -
<span style="color: #7a0874; font-weight: bold;">&#40;</span> crontab -l; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;40 * * * * python /FULLPATH/notify.py&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> crontab -
<span style="color: #7a0874; font-weight: bold;">&#40;</span> crontab -l; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;50 * * * * python /FULLPATH/notify.py&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">|</span> crontab -</pre></div></div>

<p>Check your crontab:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">crontab <span style="color: #660033;">-l</span></pre></div></div>

<p>you should see the lines with &#8220;python /FULLPATH/notify.py&#8221;.</p>
<p>To remove it from your crontab just call:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">crontab <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> notify.py <span style="color: #000000; font-weight: bold;">|</span> crontab -</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.le-web.org/2008/11/06/pygtk-how-to-display-a-systray-icon-from-a-cronjob/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

