简体   繁体   English

使用 cron 每分钟执行一次 python 3 脚本时遇到问题

[英]Trouble executing a python 3 script every minute with cron

I am attempting to run a python 3 script every 1 minute using cron on a raspberrypi 3, for testing, where eventually it will just be run once a day.我试图在 raspberrypi 3 上使用 cron 每 1 分钟运行一次 python 3 脚本进行测试,最终它只会每天运行一次。

To start, I made a new cron job using: sudo crontab -e , and typed in the following code for a once a minute job:首先,我使用以下sudo crontab -e创建了一个新的 cron 作业: sudo crontab -e ,并为每分钟一次的作业输入以下代码:

*/1 * * * * /home/pi/folder/file.py

Then I saved and closed and waited.然后我保存并关闭并等待。 My python script emails me text when executed, so I should have seen an email come in. It runs fine (and emails me) when I execute it manually outside of cron.我的 python 脚本在执行时给我发电子邮件,所以我应该看到一封电子邮件进来。当我在 cron 之外手动执行它时,它运行良好(并给我发电子邮件)。

So, what am I doing wrong with cron for it not to run?那么,我用 cron 做错了什么让它不运行? And do I need to make the python file executable or something with chmod ?我是否需要使 python 文件可执行或使用chmod

Possible duplicate of Execute python Script on Crontab 在 Crontab执行 python 脚本的可能重复

EDIT: Adding comment here since the comment box mangled my formatting.编辑:在此处添加评论,因为评论框破坏了我的格式。

In your example above it looks like you are just trying to "run" the file.在上面的示例中,您似乎只是想“运行”该文件。 You need to call the python executable, and pass it an argument that points to your file.您需要调用 python 可执行文件,并将指向您的文件的参数传递给它。

From the StackOverflow comment mentioned above look at this crontab entry:从上面提到的 StackOverflow 评论中查看这个 crontab 条目:

*/2 * * * * /usr/bin/python /home/souza/Documets/Listener/listener.py

Take a look at the first part of the command /usr/bin/python this is pointing to the python executable not just to the .py file you want to run.看看命令/usr/bin/python的第一部分,它指向 python 可执行文件,而不仅仅是指向要运行的 .py 文件。

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

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