如何在 Raspberry Pi 完全启动后自动 运行 Python 脚本?

How to automatically run Python script AFTER Raspberry Pi fully boots?

我的脚本是 /home/hello.py,它使用的是 Bottle 网络框架。在 Raspberry Pi 连接到我的本地网络后,我的脚本需要 运行 。如果我尝试在启动时自动 运行 它,它不起作用,因为网络连接还没有准备好。

我知道我可以使用 "crontab -e" 并添加 @reboot sleep 60 秒 -- 但我认为睡眠不太可靠。

我看到的所有其他 "run script on boot" 问题都没有帮助,因为它们 运行 脚本在启动过程中过早。

有没有办法检查 raspberry pi 是否已连接,然后自动让我的 python 脚本 运行?

您可以在 "all_interfaces_up" 之后的“/etc/network/if-up.d/upstart”处添加您的代码,如下所示:

all_interfaces_up() {
    python /your/code/path/codename.py
    # return true if all interfaces listed in /etc/network/interfaces as 'auto'
    # are up.  if no interfaces are found there, then "all [given] were up"

其中“/your/code/path/codename.py”是您的代码位置

还没有对以前的解决方案发表评论的声誉,而不是建议

all_interfaces_up() {
/your/code/path/codename.py
# return true if all interfaces listed in /etc/network/interfaces as 'auto'
# are up.  if no interfaces are found there, then "all [given] were up"

试试这个:

all_interfaces_up() {
python /your/code/path/codename.py
# return true if all interfaces listed in /etc/network/interfaces as 'auto'
# are up.  if no interfaces are found there, then "all [given] were up"

当您尝试 运行 一个 python 脚本时,它需要用 python 执行。