Crontab με Python και γραφικά

...εγκαταστάσεις, αναβαθμίσεις, διαχείριση partitions, boot-loader, κλπ

Συντονιστής: adem1

Κανόνες Δ. Συζήτησης
Στην ενότητα αυτή γράφουμε θέματα/ προβλήματα μόνο για το UBUNTU ή τα παράγωγά του.

Crontab με Python και γραφικά

Δημοσίευσηαπό RadwanGR » 19 Ιουν 2016, 00:21

Θέλω να ρυθμίσω το Crontab να εκτελεί ένα python σκρίπτ, το οποίο χρησιμοποιεί το appindicator3 για δημιουργεί μια ειδοποίηση στo μενού εργασίας (tray). Το πρόβλημα είναι πως το Crontab (απ´ ότι έχω καταλάβει) έχει διαφορετικές τιμές περιβάλλοντος, οπότε χρειάζεται να ορίζω "DISPLAY=:0.00" για να εκτελώ εφαρμογές με γραφικά, το οποίο δούλεψε με ένα πρόχειρο bash σκρίπτ που έφτιαξα με Zenity, όμως όχι στο python σκρίπτ μου!

Το Crontab που έχω ρυθμίσει, και εκτελείται, είναι :
Κώδικας: Επιλογή όλων
* * * * * export DISPLAY=:0.00 && /usr/bin/python '/home/radubuntu/.RMN/RootedMailNotifier.py'

Και το Python σκρίπτ είναι :
Κώδικας: Επιλογή όλων
#!/usr/bin/python

import os, sys, getpass, imaplib
from gi.repository import Gtk as gtk
from gi.repository import AppIndicator3 as appindicator

USER_MAIL = 'xxxxxxx'
USER_PASS = 'xxxxxxx'

if ('USER_MAIL' in locals()) and ('USER_PASS' in locals()):
global PROV_LINK
global PROV_IMAP
global MAILS_UNREAD
if 'gmail' in USER_MAIL:
PROV_LINK = 'http://www.gmail.com'
PROV_IMAP = 'imap.gmail.com'
elif 'yahoo' in USER_MAIL:
PROV_LINK = 'https://mail.yahoo.com'
PROV_IMAP = 'imap.mail.yahoo.com'
elif ('outlook' in USER_MAIL) or ('hotmail' in USER_MAIL):
PROV_LINK = 'https://www.outlook.com'
PROV_IMAP = 'imap-mail.outlook.com'
else:
os.system('notify-send "RootedMailNotifier" "Mail provider not supported yet"')

try:
x = imaplib.IMAP4_SSL(PROV_IMAP, 993)
x.login(USER_MAIL, USER_PASS)
x.select('INBOX')
status, response = x.status('INBOX', "(UNSEEN)")
MAILS_UNREAD = int(response[0].split()[2].strip(').,]'))
except :
os.system('notify-send "RootedMailNotifier" "The username/password specified is not valid"')
if 'gmail' in USER_MAIL:
ER_MSG1 = "zenity --error --no-wrap --height=40 --title=\'RML Rooted Mail Notifier\' "
ER_MSG2 = "--text=\'ER: ER200 GMAIL ACCESS DENIED\n\nRML access to Gmail denied.\nPlease go to the links below and check your "
ER_MSG3 = "settings.\n\n<a href=\"https://www.google.com/settings/security/lesssecureapps\">https://www.google.com/settings/security/lesssecure"
ER_MSG4 = "apps&#10138;</a>\n<a href=\"https://accounts.google.com/DisplayUnlockCaptcha\">https://accounts.google.com/DisplayUnlockCaptcha&#10138;</a>\'"
os.system((ER_MSG1 + ER_MSG2 + ER_MSG3 + ER_MSG4))

if MAILS_UNREAD > 0 :
#
global APPINDICATOR_ID
global APPINDICATOR_IMG
global APPINDICATOR_NUM
APPINDICATOR_NUM = MAILS_UNREAD
APPINDICATOR_ID = 'RootedMailNotifier'
APPINDICATOR_IMG = '/usr/share/icons/ubuntu-mono-dark/status/24/indicator-messages.svg'
#APPINDICATOR_IMG = '/usr/share/icons/hicolor/scalable/status/indicator-messages.svg'
#APPINDICATOR_IMG = '/usr/share/icons/hicolor/16x16/status/indicator-messages-new.png'
#APPINDICATOR_IMG = '/usr/share/icons/oxygen/16x16/places/mail-message.png'
#
def indicator_main():
indicator = appindicator.Indicator.new(APPINDICATOR_ID, os.path.abspath(APPINDICATOR_IMG), appindicator.IndicatorCategory.SYSTEM_SERVICES)
indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
indicator.set_menu(indicator_build_menu())
gtk.main()
#
def indicator_build_menu():
menu = gtk.Menu()
item_name = 'Open' + '(' + str(MAILS_UNREAD) + ')'
item_open = gtk.MenuItem(item_name)
item_open.connect('activate', indicator_open)
item_quit = gtk.MenuItem('Quit')
item_quit.connect('activate', indicator_quit)
menu.append(item_open)
menu.append(item_quit)
menu.show_all()
return menu
#
def indicator_open(source):
x = 'xdg-open ' + PROV_LINK
os.system(x)
#
def indicator_quit(source):
gtk.main_quit()
#
indicator_main()
#
else:
os.system('notify-send "RootedMailNotifier" "The username/password specified is not valid"')

exit()
Εμπειρία : ★★★☆☆ Αγγλικά : ★★★☆☆ Προγ/σμός: python
Spoiler: show
OS : Ubuntu 14.04.3 LTS / win 7
CPU : AMD A4-5300 APU - 2xCore 3,4 GHz FM2
GPU : ATI (Radeon) HD 6570 2G
RAM : 4GB DDR3
HDD : SEAGATE 500GB SATA3 16MB
Άβαταρ μέλους
RadwanGR
babeTUX
babeTUX
 
Δημοσιεύσεις: 72
Εγγραφή: 05 Αύγ 2014, 01:45
Εκτύπωση

Επιστροφή στο Λειτουργικό Σύστημα