尝试从 ubuntu crontab 运行 python 脚本 [英] Trying to run a python script from ubuntu crontab
问题描述
嘿,我在尝试使用来自 ubuntu 的 Python 脚本运行 cron 作业时遇到了问题.这就是我所做的:
Hey I am running into an issue when trying to run a cron job with a python script from ubuntu. This is what I have done:
1.) 编写了一个简单的 tkinter 应用程序:代码来源来自这个 url - http://www.ittc.ku.edu/~niehaus/classes/448-s04/448-standard/simple_gui_examples/sample.py
1.) Wrote a simple tkinter app: source for the code is from this 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.) 将脚本更改为可执行:
2.) changed the script to become executable:
chmod 777 sample.py
3.) 将脚本添加到我的 cronjob 中,每分钟运行一次以进行测试.我打开 crontab -e 并将以下内容添加到我的文件中:
3.) Added the script to my cronjob to be run every minute for testing purposes. I opened crontab -e and added the following to my file:
* * * * * /home/bbc/workspace/python/tkinter/sample.py
4.) 免责声明:我没有为 tkinter 添加任何额外的环境变量,也没有在/etc/init.d/cron 中更改我的 cronjob 脚本
4.) Disclaimer: I did not add any additional environment variables for tkinter nor did I change my cronjob script at /etc/init.d/cron
5.) 我通过执行 tail -f/var/log/syslog
5.) I was tracking the cron job by doing a tail -f /var/log/syslog
$ 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)
任何有关调试此问题的帮助将不胜感激...
Any help on debugging this issue will be most appreciated...
推荐答案
我不确定您希望在这里发生什么.cronjob 将无法访问可以显示 GUI 的显示器,因此永远不会显示按钮,因此永远不会运行 print_this
I'm not sure what you expect to happen here. The cronjob won't have access to a display where it can display the GUI, so the button will never be displayed, so print_this
will never be run
FWIW,当我尝试运行您的代码时出现错误:
FWIW, when I tried to run your code I got an error:
File "./t.py", line 4
def __init__(self,parent):
^
IndentationError: expected an indented block
不确定这是否只是由复制/粘贴到页面中引起的,或者是否是您的代码的真正问题.
Not sure if that's just caused by copy/paste into the page or if it's a real problem with your code.
这篇关于尝试从 ubuntu crontab 运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!