简体   繁体   English

Crontab 未在 Ubuntu 上执行 Python 脚本

[英]Crontab not executing Python script on Ubuntu

I have a Python script with the following code:我有一个包含以下代码的 Python 脚本:

import subprocess
import sys

default = "Take a 20 second break - look at least 20 feet away!"
message = sys.argv[1] if len(sys.argv) > 1 else default

def sendmessage(message):
    subprocess.Popen(['notify-send', message])
    return

sendmessage(message)

called takebreak.py , which will send a system notification.称为takebreak.py ,它将发送系统通知。

When I try to automate it using crontab like so:当我尝试使用crontab使其自动化时,如下所示:

* * * * * /usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py

It doesn't work.它不起作用。 Running the command运行命令

/usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py

in the terminal does work, which means it's not a file location problem, but rather something to do with cron .在终端中确实有效,这意味着它不是文件位置问题,而是与cron Any ideas?有任何想法吗?

EDIT1:编辑1:

After debugging and looking at the logs, I can verify that cron is actually executing the commmand export DISPLAY=:0; /usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py在调试和查看日志后,我可以验证 cron 确实在执行命令export DISPLAY=:0; /usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py export DISPLAY=:0; /usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py every minute like I set it to do, but for some reason this command, while it should send a system notification, is not doing so. export DISPLAY=:0; /usr/bin/python /home/polo/git-repositories/takebreak/takebreak.py每分钟就像我设置的那样,但由于某种原因,这个命令虽然应该发送系统通知,但并没有这样做。 Any ideas?有任何想法吗?

EDIT2:编辑2:

The solution was to add some address bus thing (forget the exact code) that I found in another post, that ended up fixing it.解决方案是添加我在另一篇文章中找到的一些地址总线的东西(忘记确切的代码),最终修复了它。 Unfortunately, none of the answers or comments here helped with solving the problem, but thanks regardless!不幸的是,这里的答案或评论都没有帮助解决问题,但无论如何都感谢!

Most likely, the problem is that notify-send is not in your $PATH when running from crontab .最有可能的问题是,从crontab运行时, notify-send不在您的$PATH First, figure out where it's stored:首先,弄清楚它的存储位置:

$ which notify-send
/usr/bin/notify-send

For me, it's in /usr/bin .对我来说,它在/usr/bin

At the top of your crontab file ( crontab -e ), set $PATH :crontab文件 ( crontab -e ) 的顶部,设置$PATH

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

If you want to include whatever $PATH may have already been set before (safer), do this instead:如果您想包含之前可能已经设置的任何$PATH (更安全),请改为执行以下操作:

PATH="${PATH}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Make sure this includes the directory where your command is installed, if it's not installed in /usr/bin .如果它没有安装在/usr/bin ,请确保这包括安装命令的目录。

Of course, the other option, is to simply specify the full command path in your Python script:当然,另一种选择是简单地在 Python 脚本中指定完整的命令路径:

subprocess.Popen(['/usr/bin/notify-send', message])

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

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