位于RC.Local的Python脚本在Raspbian上没有开始启动



这是我的python脚本(main.py):

#! /usr/bin/env python
import time
# ..... some file imports from the same folder .....
try:
    # .... Some setup code
    while True:
        if turnOffRequestHandler.turnOffIsRequested():
            break;
        time.sleep(1)
except BaseException as e:
    pass
finally:
    # ..... Some code to dispose resources

我尝试在每个创业公司上调用它的方式是首先编辑rc.local与此相似:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %sn" "$_IP"
fi
python3 /home/pi/Desktop/ProjectFolder/sample/main.py &
exit 0

,然后通过导航到包含目录并执行以下命令来使我的Python脚本可执行:

chmod 755 main.py

然后,我希望在重新启动系统后将我的脚本运行。我无法分辨它是否运行。我能说的是,我应该调用一些Web端点。我现在想知道它是否真的被执行,但wifi尚未连接。

我该如何诊断?因为,当我尝试手动执行(在系统启动并连接到WiFi之后)时:

pi@raspberrypi:~ $ /etc/rc.local

它正在开始,一切都按预期工作。

编辑:有可能与我尝试执行的脚本有关的事实与位于同一文件夹中的文件有关/etc/..)?.

我会尝试使用'echo'shosing'>> to_file'。

的简单bash脚本

在使用脚本时,您可以将输出(例如"打印")重定向到文件。这样更改您的脚本:

python3 /home/pi/Desktop/ProjectFolder/sample/main.py &> logfile.txt

,在您的文件中,使用普通打印来在文件中打印出1个问题。您必须在代码运行时冲洗输出才能查看输出文件:

import sys
sys.stdout.flush()

编辑

如果您使用的是python 3.3或更高,则有一种替代方法 - 打印具有冲洗输出的参数。

最新更新