python - 启动时运行的脚本不记录输出

标签 python linux bash raspberry-pi startup

我一直在尝试在 Raspberry Pi 启动时运行以下代码:

#!/usr/bin/python3

import numpy
import math
import cv2
#this is python 3 specific
import urllib.request
from enum import Enum
from VisionProcessor import VisionProcessor
from GripPipeline import GripPipeline
from networktables import NetworkTables
import time
import logging
from networktables.util import ntproperty

#proper networktables setup
logging.basicConfig(level=logging.DEBUG)
NetworkTables.initialize(server='10.17.11.76')

#create the field to talk to on the network table
class NTClient(object):
    angle_difference = ntproperty('/Raspberry Pi/angle difference', 0)
    distance_from_target = ntproperty('/Raspberry Pi/distance from target', 0)

n = NTClient()

frame = cv2.VideoCapture('https://frc:frc@10.17.11.11/mjpg/video.mjpg')

if(frame == None):
    print("error: camera not found. check connection")
#pipeline = GripPipeline()
pipeline = VisionProcessor()


print("pipeline created")

def get_image():
    ret, img_array = frame.read()
#    cv2.imwrite("frame.jpg", img_array)
    return img_array

def find_distance(width, height, y):
    #distances are in inches
    KNOWN_WIDTH = 6.25
    KNOWN_DISTANCE = 12.0
    KNOWN_PIXELS = 135.5
    KNOWN_HEIGHT = 424.0

    focal_length = (KNOWN_PIXELS * KNOWN_DISTANCE)/KNOWN_WIDTH
    #hypotenuse = (KNOWN_WIDTH * focal_length)/width
    distance = (KNOWN_WIDTH * focal_length)/width

    #0.2125 degrees per pixel vertical
#    theta = (0.2125) * (240 - y)

#    distance = KNOWN_HEIGHT * (math.tan((math.pi / 2) - math.radians(theta)))

    return distance

x = True
while x:
    print("while loop entered")
    img = get_image()
    print("image gotten")
    center_point = [160, 120]
    file = open('output.txt', 'a')
    try:
        current_point, size, y = pipeline.process(img)
        #negative means turn left, positive means turn right
        pixel_difference = center_point[0] - current_point[0]
        #4.7761 pixels per degree
        angle_difference = (float)(pixel_difference) / 4.7761
        n.angle_difference = angle_difference
        target_width = size[0]
        target_height = size[1]
        distance = find_distance(target_width, target_height, y)
        n.distance_from_target = distance
        print("angle")
        file.write("angle: ")
        print(n.angle_difference)
        file.write(str(angle_difference))
        print(" distance: ")
        file.write("distance")
        print(distance)
        file.write(str(distance))
        file.write("\n")
    except UnboundLocalError:
        print(":(")
    except (TypeError, cv2.error) as e:
        print(":(")

#    x = False

我一直在通过编辑 /etc/rc.local 文件来执行此操作,并且脚本已“成功”运行。 Pi 在启动时显示大约 25% 的 CPU 使用率,并且在脚本运行时它保持一致,所以我可以看到它何时处于事件状态(我没有在这个 Pi 上运行任何其他进程)。使用 ps -aux 显示事件的 python3 进程。但是,它没有向 output.txt 文件或网络表输出任何内容。

我的最终目标是让它成功输出到网络表。如果我正常运行它(例如,不是在启动时,通过终端中的 python3 pipeline-test.py),它会正确地输出到 output.txt 和网络表。我添加了 output.txt 以确保获得正确的输出,并且它工作正常,除非它在启动时运行。

有没有人知道哪里出了问题?如果需要更多信息,我会尽力提供。

编辑:出于某种原因,当我从 Github 复制我的代码时,它丢失了所有缩进。使用的密码是here .

最佳答案

首先,/etc/rc.local 脚本以 root 身份执行,因此位于根目录中。您需要将完整的文件路径添加到您的 Python 程序中。这可能会或可能不会解决问题。

python /dir/dir/python_program

您可以将此程序的输出记录在错误文件中。制作文件

sudo nano /home/pi/error.log

在该文件中,只需键入任何内容,然后退出 (ctrl + x) 保存更改。然后编辑 rc.local 以便将消息附加到文件中

python /dir/dir/python_program > /home/pi/error.log &

现在执行重启

sudo reboot

pi 将启动并运行程序,几分钟后,pkill python 并查看/home/pi/error.log 文件。这将使您更好地了解程序“失败状态”的情况

我注意到在你的程序中你调用了一个文件。您需要文件的完整路径而不是 output.txt,因为程序在启动时在根目录中执行。这将需要在您的程序调用任何文件的所有情况下进行更改。

如果您随后在日志文件中收到权限错误,请运行以下命令

sudo chmod 777 -R /filepath_to_your_script

关于python - 启动时运行的脚本不记录输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42285356/

相关文章:

python 3 : Making a str object callable

python 2 : Compare a timestamp between two points in time

linux - 使用 let 命令的多个间接变量 (GNU/Linux Bash)

java - 在 java|groovy 中将字符串放入列表的正确方法

regex - 匹配输入字符串中的 URL 链接

linux - 从 Perl 程序执行 bash 脚本

bash - 将并行变量 "{}"作为 awk 变量传递

Python Twisted - 预期代理和服务器端延迟

python - 如何转换 Dataframe Column1 :Column2 (key:value) in Dictionary in Pyspark?

c++ - 是否可以缓存从 MapViewOfFile 返回的映射区域?