尝试从ubuntu crontab运行python脚本

发布于 2021-01-29 16:17:18

嘿,尝试从ubuntu用python脚本运行cron作业时遇到问题。这是我所做的:

1.)编写了一个简单的tkinter应用程序:代码的来源是从此url-
http://www.ittc.ku.edu/~niehaus/classes/448-s04/448-standard/simple_gui_examples/sample.py

#!/usr/bin/python
from Tkinter import *
class App:
    def __init__(self,parent):
        f = Frame(parent)
        f.pack(padx=15,pady=15)
        self.entry = Entry(f,text="enter your choice")
        self.entry.pack(side= TOP,padx=10,pady=12)
        self.button = Button(f, text="print",command=self.print_this)
        self.button.pack(side=BOTTOM,padx=10,pady=10)
        self.exit = Button(f, text="exit", command=f.quit)
        self.exit.pack(side=BOTTOM,padx=10,pady=10)

    def print_this(self):
        print "this is to be printed"

root  = Tk()
root.title('Tkwidgets application')
app = App(root)
root.mainloop()

2.)将脚本更改为可执行文件:

chmod 777 sample.py

3.)将脚本添加到我的cronjob中,以便每分钟运行一次以进行测试。我打开crontab -e并将以下内容添加到我的文件中:

 * * * * * /home/bbc/workspace/python/tkinter/sample.py

4.)免责声明:我没有为tkinter添加任何其他环境变量,也没有在/etc/init.d/cron中更改cronjob脚本。

5.)我通过执行tail -f / var / log / syslog来跟踪cron作业

$ tail -f /var/log/syslog
Jul  7 18:33:01 bbc CRON[11346]: (bbc) CMD (/home/bbc/workspace/python/tkinter/sample.py)
Jul  7 18:33:01 bbc CRON[11343]: (CRON) error (grandchild #11344 failed with exit status 1)
Jul  7 18:33:01 bbc CRON[11343]: (CRON) info (No MTA installed, discarding output)
Jul  7 18:33:01 bbc CRON[11342]: (CRON) error (grandchild #11346 failed with exit status 1)
Jul  7 18:33:01 bbc CRON[11342]: (CRON) info (No MTA installed, discarding output)

非常感谢您提供调试此问题的任何帮助…

关注者
0
被浏览
61
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    我不确定您希望在这里发生什么。cronjob将无法访问可以在其中显示GUI的显示器,因此该按钮将永远不会显示,因此print_this也永远不会运行

    FWIW,当我尝试运行您的代码时,出现错误:

      File "./t.py", line 4
        def __init__(self,parent):
          ^
    IndentationError: expected an indented block
    

    不知道这是由复制/粘贴到页面中引起的,还是代码真正的问题。



推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看