简体   繁体   中英

Zenity not working in python script with crontab

I have a python script in which I have used Zenity to display some notification. The code snippet is as follows:

if message_list:
    pretty_print(message_list)
    os.system("/usr/bin/zenity --notification --text='You have unread messages'")

When I run this script normally, everything works fine ie dialog box appears and message gets displayed. But when I schedule this script in crontab nothing appears. Any solution to this?

There is no sane way to run interactive commands from cron . There is no guarantee that there is a user, there is no guarantee that there is a single user, there is no guarantee that the user(s) who are on want to, or are even able to, interact with your software; some of them may be pseudo-users or remote on expensive metered Internet access or just idle or whatever.

The usual solution is a server/client architecture where whatever runs from cron makes its results available via some IPC mechanism, and users who want the results run a client from within their X11 session (or shell, or what have you).

Create a script info.sh (remember to grant it execute rights):

#!/bin/bash
xhost +
/usr/bin/zenity --notification --text='You have unread messages'

And in your script:

if message_list:
    pretty_print(message_list)
    os.system("./info.sh")

That's if you want to use the solution you mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM