如何成功启动 python cronjob?
How to successful launch a python cronjob?
在我看来这是一个简单的任务,但我不知道如何实现它。
我有一个 python 脚本,它可能会在前几次尝试时崩溃。
(它正在调用一个尚未准备好的 websocket。)
我可以告诉 cronjob 继续尝试 运行 这个脚本直到它最终起作用吗?或者我是否必须在 python 脚本本身中实现它?
这是 Crontab 中的行
@reboot sh /home/pi/launcher.sh
这是 .sh 文件。
#!/bin/sh
# launcher.sh
cd /
sudo python /home/pi/example.py
cd /
无论具体任务是什么,您最好在 python 脚本中实现它。这似乎更直接。例如:
while True:
try:
# your task
# check if it was successful and if so
break
except Exception: # a relevant exception would be best
pass # you could even use sleep() here(time module)
# to wait a few seconds between tries
在我看来这是一个简单的任务,但我不知道如何实现它。
我有一个 python 脚本,它可能会在前几次尝试时崩溃。 (它正在调用一个尚未准备好的 websocket。)
我可以告诉 cronjob 继续尝试 运行 这个脚本直到它最终起作用吗?或者我是否必须在 python 脚本本身中实现它?
这是 Crontab 中的行
@reboot sh /home/pi/launcher.sh
这是 .sh 文件。
#!/bin/sh
# launcher.sh
cd /
sudo python /home/pi/example.py
cd /
无论具体任务是什么,您最好在 python 脚本中实现它。这似乎更直接。例如:
while True:
try:
# your task
# check if it was successful and if so
break
except Exception: # a relevant exception would be best
pass # you could even use sleep() here(time module)
# to wait a few seconds between tries