python脚本的Cron调度程序使用notify-send [英] Cron scheduler of python script using notify-send

查看:662
本文介绍了python脚本的Cron调度程序使用notify-send的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用这三种技术时遇到了一些困难。

I'm having some difficulty using these three technologies together.

Cron条目:

* * * * * /usr/bin/python /path/to/python/email/program.py

Python程式:

  1 #!/usr/bin/python
  2 
  3 import imaplib
  4 import os
  5 import sys
  6 import pynotify
  7 
  8 if not pynotify.init('Emails'):
  9     sys.exit(1)
 10 
 11 with open('/path/to/python/email/count.tmp', 'r') as file:
 12     data = int(file.read().strip())
 13 client = imaplib.IMAP4_SSL('mail.sever.com', '993')
 14 client.login('user@server.com', 'password')
 15 client.select()
 16 
 17 unseen = client.search(None, 'UnSeen')[1][0].split()
 18 
 19 if unseen[0] == '':
 20     pass
 21 else:
 22     if len(unseen) != data:
 23         n = pynotify.Notification(str(len(unseen) - data) + " New Messages",
 24             str(len(unseen)) + " Unread Messages",
 25             "file:///path/to/python/email/mail.png")
 26         if not n.show():
 27             print "Fail"
 28             sys.exit(1)
 30         with open('/path/to/python/email/count.tmp', 'w') as file:
 31             file.write(str(len(unseen)))

脚本本身运行时可以正常工作,但在计划为cron作业时不会运行。我检查了syslog,它说脚本正在运行,我以sudo的方式运行日志中的行来验证。

The script works correctly when run by itself, but it will not run when scheduled as a cron job. I checked the syslog and it says the script is being run, and I've run as sudo the line from the log to verify.

我已检查过

在Crontab上执行python脚本

Cron with notify-send

,但两者都没有,也没有进一步的链接似乎解决这个组合。

but neither, nor further links seem to address this combination.

有任何想法吗?

由于pynotify似乎不允许程序工作,我已经替换他们的调用与os.system调用。至少这会更新小tmp文件,但仍然没有通知。

As pynotify seems not to allow the program to work at all, I've replaced their calls with an os.system call. At least this updates the small tmp file, but still no notify.

os.system('/usr/bin/X11/notify-send "{} New Messages" "{} Unread Messages"'.format(len(unseen) - data, len(unseen))



-Update 2 -



-Update 2-

/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
warnings.warn(str(e), _gtk.Warning)

** (other.py:16509): WARNING **: Command line `dbus-launch --autolaunch=91bdcc4a583bfb56734fbe1200000010 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n


推荐答案

手动运行程序时, DISPLAY 正被设置为当前显示(在大多数情况下为:0 ),但是当通过cron运行脚本时,没有设置此类变量,并且通知不起作用。

When you run your program manually, it works because the variable DISPLAY is being set to your current display (:0 in most cases), however when running the script via cron, there is no such variable being set and notifications doesn't work.

正如此回答,您应该导出显示和Xauthority,最后以您的用户身份运行脚本(而不是以root用户身份运行)。

As mentionned in this answer, you should export both the display and the Xauthority, and finally running the script as your user (and not as root).

像这样:

* * * * * export DISPLAY=:0.0 && export XAUTHORITY=/home/<username>/.Xauthority && sudo -u <username> /usr/bin/python /path/to/python/email/program.py

code><用户名> 与您的用户名)

(change <username> with your username)

这篇关于python脚本的Cron调度程序使用notify-send的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆