简体   繁体   English

无法使用cron运行python脚本

[英]Unable to run python script with cron

I am trying to run a simple python script with cron and I cannot for the life of me get it to work. 我试图用cron运行一个简单的python脚本,但我终生无法正常工作。 I am running Ubuntu 18.04. 我正在运行Ubuntu 18.04。

I know there are many questions (and answers) on this. 我知道对此有很多疑问(和答案)。 I have looked at a number of them and I still cannot get it to work. 我看过其中的一些,但仍然无法正常工作。

My python script is: 我的python脚本是:

#!/usr/bin/env python

from tkinter import messagebox
messagebox.showinfo('MessageBox','Hello world!')

I have followed Unable to Run Python Script In Cron and changed the permissions as follows: 我关注了“ 无法在Cron中运行Python脚本 ”并按如下所示更改了权限:

chmod +x /home/brb/bugs/projects/cron/hello.py

I can successfully run this from the command line as follows: 我可以从命令行成功运行此代码,如下所示:

brb@bishop:~$ /home/brb/bugs/projects/cron/hello.py 

Inside crontab I have tried to execute the script in 2 different ways as follows (every 2 or 3 minutes to try and pin down which one works...) 在crontab内,我尝试按照以下两种不同方式执行脚本(每2或3分钟尝试确定一个有效的脚本...)

*/2 * * * * /home/brb/bugs/projects/cron/hello.py
*/3 * * * * /usr/bin/env python /home/brb/bugs/projects/cron/hello.py

Nothing happens. 什么都没发生。

I followed http://www.jessicayung.com/automate-running-a-script-using-crontab/ and tried to inspect the error file as follows: 我遵循了http://www.jessicayung.com/automate-running-a-script-using-crontab/并尝试检查错误文件,如下所示:

cd ../..
cd var/mail 
nano $brb

and I observe a message in nano that says 我在nano中观察到一条消息

[ Directory '.' is not writable ]

I am not sure which directory '.' 我不确定哪个目录是“。” is, perhaps it is var/mail. 是,也许是var / mail。 In any case, not sure how to make it writable. 无论如何,不​​确定如何使其可写。 I will google chmod some more to get this writable I guess, but that still doesn't solve why cron is not working when it looks like all the examples I've seen... 我会在chmod上添加更多Google chmod,以使它可写,但是当我看到所有示例时,cron仍无法解决为什么cron无法正常工作...

EDIT: 编辑:

I followed How to run an X program from outside the X session (eg from the console or SSH) as per the comment from triplee and, not really knowing what I am doing, I attempted to change my display permissions as follows: 我按照如何从X会话外部(例如从控制台或SSH)在X会话外部运行X程序,按照三元组的注释进行操作,并且由于不知道自己在做什么,因此尝试更改显示权限,如下所示:

brb@bishop:~$ export XAUTHORITY=/home/brb/.Xauthority
brb@bishop:~$ export DISPLAY=':0'

My cron job is still not work... 我的Cron工作仍然无法正常工作。

Your problem is twofold: 您的问题是双重的:

  • cronjob has does not know where it could be displaying a graphical app (which X server or DISPLAY to use. cronjob具有不知道它可能在哪里显示图形应用程序(要使用哪个X服务器或DISPLAY

    This can be addressed by making sure DISPLAY is set in the environment of you script. 通过确保在脚本环境中设置了DISPLAY可以解决此问题。 Run echo $DISPLAY in a terminal running in your X session. 在X会话中运行的终端中运行echo $DISPLAY :0 is a very likely it. :0很有可能。

  • Even if it knows the correct DISPLAY , it does (should) not have access to it. 即使知道正确的DISPLAY ,它也(应该)没有访问权限。 If you are running the cronjob under the same user as the one running your target X session. 如果您在与运行目标X会话的用户相同的用户下运行cronjob。 If it is a different user, and it has access to your user's home, you could reuse it's authority file by setting XAUTHORITY to point to it. 如果它是另一个用户,并且可以访问用户的家,则可以通过将XAUTHORITY设置为指向它的权限文件来重用它的权限文件。

    You can also export necessary information from your running session (renewed with each session), type this into a terminal there: xauth list $DISPLAY and add it for your cron running user: xauth add displayname protocolname hexkey (using those three values as returned by xauth list . 您还可以从正在运行的会话中导出必要的信息(随每个会话更新),将其输入到该终端中: xauth list $DISPLAY并将其添加给您的cron运行用户: xauth add displayname protocolname hexkey (使用以下三个值返回) xauth list

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

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