[英]Why my crontab request dont work to call a python script
On Raspberry Pi 3B+ running on Raspberry Pi OS 64. Trying to make a Python script executed every minute but don't work.在 Raspberry Pi OS 64 上运行的 Raspberry Pi 3B+ 上。试图使 Python 脚本每分钟执行一次,但不起作用。 To edit crontab, I use:
要编辑 crontab,我使用:
sudo crontab -e
And put this line in file:并将此行放入文件中:
*/1 * * * * sudo /bin/python3 /home/pi/Documents/script_test01.py`
I also tried: */1 * * * * sudo python3 /home/pi/Documents/script_test01.py
我也试过:
*/1 * * * * sudo python3 /home/pi/Documents/script_test01.py
Here my script, simply publish in a MQTT broker (script works by direct call in shell: python3 script_test01.py
):这是我的脚本,只需在 MQTT 代理中发布(脚本通过直接调用 shell:
python3 script_test01.py
):
#!/usr/bin/env python3
import time
import paho.mqtt.client as mqtt
client_mqtt = mqtt.Client("Script-crontab")
client_mqtt.connect("localhost")
client_mqtt.publish("RandomTempValuesSimulator", "Hello !")
client_mqtt.disconnect()
exit()
I did a stop and start cron service with:我停止并启动了 cron 服务:
sudo systemctl stop cron.service
sudo systemctl start cron.service
Nothing more happened.什么也没发生。
Make sure your script is executable.确保您的脚本是可执行的。 The interpreter is usually at
/usr/bin/python3
(not /bin/python3
) but you don't need that since you have the #!
解释器通常位于
/usr/bin/python3
(不是/bin/python3
)但你不需要它,因为你有#!
on top of your script.在你的脚本之上。
Why are you editing root
's crontab?你为什么要编辑
root
的 crontab? Why not just your own?为什么不只是你自己的? Check your
/var/log/syslog
files for any errors in the execution.检查您的
/var/log/syslog
文件以查找执行中的任何错误。
Solved.解决了。 When i did
crontab -l
, i've got no crontab for pi user
.当我执行
crontab -l
时,我no crontab for pi user
。
Problem solved without "sudo" with command crontab -e
and edit the blank crontab file openned and put this command: */1 * * * * /bin/python3 /home/pi/Documents/script_test01.py
使用命令
crontab -e
解决了没有“sudo”的问题并编辑打开的空白 crontab 文件并输入此命令: */1 * * * * /bin/python3 /home/pi/Documents/script_test01.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.