简体   繁体   中英

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. To edit crontab, I use:

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

Here my script, simply publish in a MQTT broker (script works by direct call in 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:

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 #! on top of your script.

Why are you editing root 's crontab? Why not just your own? Check your /var/log/syslog files for any errors in the execution.

Solved. When i did crontab -l , i've got 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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