猿问

位于 Raspbian 上的 rc.local 中的 Python 脚本未在启动时启动

这是我的 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 %s\n" "$_IP"

fi

python3 /home/pi/Desktop/ProjectFolder/sample/main.py &

exit 0

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


chmod 755 main.py

然后,我希望在系统重新启动后让我的脚本运行。我不知道它是否运行。我能说的是它应该调用一些网络端点。我现在想知道它是否真的被执行了,但 wifi 还没有连接。


我怎么能诊断呢?因为,当我尝试像这样手动执行(在系统启动并连接 Wifi 之后)时:


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

它正在开始,一切都按预期进行。


编辑:是否可能与我尝试执行的脚本在其中引用位于同一文件夹中的文件(不同于 /etc/..)的事实有关?。


吃鸡游戏
浏览 348回答 2
2回答

qq_笑_17

您可以在使用脚本时将输出(如打印“某物”)重定向到文件。像这样改变你的脚本:python3 /home/pi/Desktop/ProjectFolder/sample/main.py &> logfile.txt并在您的文件中使用普通打印在您的文件中打印 1 个问题。您必须在代码运行时刷新输出才能查看输出文件:import  syssys.stdout.flush()编辑如果您使用的是 python 3.3 或更高版本,则有一种替代方法 - 打印具有刷新输出的参数。

海绵宝宝撒

我会尝试使用'echo "something" >> to_file' 的简单 bash 脚本。
随时随地看视频慕课网APP

相关分类

Python
我要回答