这是我的 Python 脚本 (main.py):#! /usr/bin/env pythonimport 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: passfinally: # ..... 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) || trueif [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP"fipython3 /home/pi/Desktop/ProjectFolder/sample/main.py &exit 0然后通过导航到包含目录并执行以下命令使我的 python 脚本可执行:chmod 755 main.py然后,我希望在系统重新启动后让我的脚本运行。我不知道它是否运行。我能说的是它应该调用一些网络端点。我现在想知道它是否真的被执行了,但 wifi 还没有连接。我怎么能诊断呢?因为,当我尝试像这样手动执行(在系统启动并连接 Wifi 之后)时:pi@raspberrypi:~ $ /etc/rc.local它正在开始,一切都按预期进行。编辑:是否可能与我尝试执行的脚本在其中引用位于同一文件夹中的文件(不同于 /etc/..)的事实有关?。
2 回答

qq_笑_17
TA贡献1818条经验 获得超7个赞
您可以在使用脚本时将输出(如打印“某物”)重定向到文件。像这样改变你的脚本:
python3 /home/pi/Desktop/ProjectFolder/sample/main.py &> logfile.txt
并在您的文件中使用普通打印在您的文件中打印 1 个问题。您必须在代码运行时刷新输出才能查看输出文件:
import syssys.stdout.flush()
编辑
如果您使用的是 python 3.3 或更高版本,则有一种替代方法 - 打印具有刷新输出的参数。
添加回答
举报
0/150
提交
取消