Comment 1 for bug 441628

Revision history for this message
cliff6056 (cliff6056) wrote :

Now, I have no idea if the following applies, but the explanation seems to fit: that when the display is active, backintime-kde works fine, however, when the display is not, it fails with no backup, and the systray icon remains frozen in place. Also, when the systray icon is frozen, a mouseover message as follows: "Compare with snapshot 2009-10-05 19:00 :01 (rsync:>F++++++++++++++++++ etx/X11/Xwrapper.config)".

My skill level is such that I have no idea if the above or below information is useful in detecting the Segmentation Fault problem originally posted or if it is related, however, hope it of assistance if anyone else has this problem. Should also point out that if I install backintime-gnome on the kde system, no systray icon ever appears, and subsequently, backintime does not experience any issues, and is the solution I use at this time.

The following is quoted from the le-web.org site: http://www.le-web.org/2008/11/06/pygtk-how-to-display-a-systray-icon-from-a-cronjob/.

PyGTK: How to display a systray icon from a cronjob

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’t access to xserver. So before importing pygtk you should check if DISPLAY is defined. If not just define it to default value “:0.0″.

import os

#if DISPLAY is not set, then set it to default ':0.0'
if len( os.getenv( 'DISPLAY', '' ) ) == 0:
 os.putenv( 'DISPLAY', ':0.0' )

import pygtk
pygtk.require("2.0")

But what happens if your gtk application really can’t connect to the xserver (xserver is not runnig, you are not logged in …). 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.

To check if your application can access to xserver just get the default display. If it is None then you can’t access it.

display = gtk.gdk.display_get_default()
if not display is None:
 ...

Now, putting all together I made this simple application: notify.py.

import os

#if DISPLAY is not set, then set it to default ':0.0'
if len( os.getenv( 'DISPLAY', '' ) ) == 0:
 os.putenv( 'DISPLAY', ':0.0' )

import pygtk
pygtk.require("2.0")
import gtk
import threading
import time

#GTK main loop thread
class GTKMainThread(threading.Thread):
 def run(self):
  gtk.main()

#get default display. None means it can't connect to xserver
display = gtk.gdk.display_get_default()
statusIcon = None

#if the display is not None show status icon
if not display is None:
 #start a gtk loop in another thread
 gtk.gdk.threads_init()
 GTKMainThread().start()

 try:
  statusIcon = gtk.StatusIcon()
  statusIcon.set_from_stock( gtk.STOCK_INFO )
  statusIcon.set_visible( True )
  statusIcon.set_tooltip(_("Back In Time: take snapshot ..."))
 except:
  pass

#do something here
print "Begin"
time.sleep( 5 ) #wait 5 seconds
print "End"

#hide status icon
if not statusIcon is None:
 statusIcon.set_visible( False )

#quit GTK main look
if not display is None:
 gtk.main_quit()

Try it:

python notify.py

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):

( crontab -l; echo "0 * * * * python /FULLPATH/notify.py" ) | crontab -
( crontab -l; echo "10 * * * * python /FULLPATH/notify.py" ) | crontab -
( crontab -l; echo "20 * * * * python /FULLPATH/notify.py" ) | crontab -
( crontab -l; echo "30 * * * * python /FULLPATH/notify.py" ) | crontab -
( crontab -l; echo "40 * * * * python /FULLPATH/notify.py" ) | crontab -
( crontab -l; echo "50 * * * * python /FULLPATH/notify.py" ) | crontab -

Check your crontab:

crontab -l

you should see the lines with “python /FULLPATH/notify.py”.

To remove it from your crontab just call:

crontab -l | grep -v notify.py | crontab -

Tags: cron, Linux, pygtk, python, systray

This entry was posted on Thursday, November 6th, 2008 at 1:51 pm and is filed under Linux, Python/PyGTK, Tips, Tricks & Scripts. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.