为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

吃鸡游戏 2021-11-23 19:59:01
这是我的 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 或更高版本,则有一种替代方法 - 打印具有刷新输出的参数。


查看完整回答
反对 回复 2021-11-23
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

我会尝试使用'echo "something" >> to_file' 的简单 bash 脚本。


查看完整回答
反对 回复 2021-11-23
  • 2 回答
  • 0 关注
  • 347 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信