简体   繁体   English

尝试从ubuntu crontab运行python脚本

[英]Trying to run a python script from ubuntu crontab

Hey I am running into an issue when trying to run a cron job with a python script from ubuntu. 嘿,尝试从ubuntu用python脚本运行cron作业时遇到问题。 This is what I have done: 这是我所做的:

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 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.) changed the script to become executable: 2.)将脚本更改为可执行文件:

chmod 777 sample.py

3.) Added the script to my cronjob to be run every minute for testing purposes. 3.)将脚本添加到我的cronjob中,以便每分钟运行一次以进行测试。 I opened crontab -e and added the following to my file: 我打开crontab -e并将以下内容添加到我的文件中:

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

4.) Disclaimer: I did not add any additional environment variables for tkinter nor did I change my cronjob script at /etc/init.d/cron 4.)免责声明:我没有为tkinter添加任何其他环境变量,也没有在/etc/init.d/cron中更改cronjob脚本。

5.) I was tracking the cron job by doing a tail -f /var/log/syslog 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)

Any help on debugging this issue will be most appreciated... 非常感谢您提供调试此问题的任何帮助...

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 cronjob将无法访问可以显示GUI的显示器,因此该按钮将永远不会显示,因此print_this将永远不会运行

FWIW, when I tried to run your code I got an error: FWIW,当我尝试运行您的代码时,出现错误:

  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. 不知道这是由复制/粘贴到页面中引起的,还是代码真正的问题。

In linux mint 17 I had to do the following: 在Linux mint 17中,我必须执行以下操作:

Make the scripts executable 使脚本可执行
~$chmod +x script.py 〜$ chmod + x script.py

You have to enable X ACL for localhost to connect to for GUI applications to work 您必须为本地主机启用X ACL以连接到GUI应用程序才能正常工作
~$ xhost +local: 〜$ xhost +本地:

Add the following line in the crontab "env DISPLAY=:0.0" 在crontab中添加以下行“ env DISPLAY =:0.0”
* * * * * env DISPLAY=:0.0 /usr/bin/python /your-script-somewhere.py * * * * * env DISPLAY =:0.0 / usr / bin / python /您的脚本-somewhere.py

And one more line to crontab ">/dev/null 2>&1" 还有一行crontab“> / dev / null 2>&1”
* * * * * env DISPLAY=:0.0 /usr/bin/python /your-script-somewhere.py >/dev/null 2>&1 * * * * * env DISPLAY =:0.0 / usr / bin / python /your-script-somewhere.py> / dev / null 2>&1

you can check errors in /var/log/syslog file 您可以在/ var / log / syslog文件中检查错误
~$ tail -20 /var/log/syslog 〜$ tail -20 / var / log / syslog

more info: 更多信息:
https://help.ubuntu.com/community/CronHowto https://help.ubuntu.com/community/CronHowto

I use crontab to run a bash file 我使用crontab运行bash文件

30 12 * * 1,2,3,4,5 /home/edward/SSF/SW/EODWD.sh 30 12 * * 1,2,3,4,5 /home/edward/SSF/SW/EODWD.sh

in termanal -- use crontab -e 在终端中-使用crontab -e

the bash file executes as many other programs as you wish bash文件可以执行任意数量的其他程序

/home/edward/SSF/SW/EODWD.py >> /home/edward/Desktop/eodmail.log wait /home/edward/SSF/SW/EODWD.py >> /home/edward/Desktop/eodmail.log等待

this example also sends all print statements in EODWD.py to a log file automatically 此示例还将EODWD.py中的所有打印语句自动发送到日志文件

the wait statement forces competion before taking the next command 在执行下一条命令之前,wait语句会强制执行竞争

this works ONLY IF both files ( *.py & *.sh ) are made executable 仅当两个文件(* .py&* .sh)都可执行时,此方法才有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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