【问题标题】:Run Python as root on boot issues在启动问题上以 root 身份运行 Python
【发布时间】:2020-04-23 20:42:58
【问题描述】:

如果已经问过这个问题,通常会提前道歉,但我过去几个小时一直在尝试以前的答案,所以我迷路了。

我有以下 python 脚本,在 pi 零上运行以检测 RFID 卡并充当 HID 键盘来输入我的密码。我知道这并不安全,但它运行良好。

当我单独 SSH 到 pi 并启动脚本时,它可以工作。我试图让它在启动时启动,但效果不太好。我已经尝试过 bashrc、init.d 和我在努力中发现的任何其他方式,因此将不胜感激任何其他帮助。

我还尝试通过 bash 脚本进行屏幕显示,担心我的脚本需要一个活动的 shell,但无济于事。

这是我的脚本:

import time
import RPi.GPIO as GPIO

import MFRC522
import signal
import datetime
now = datetime.datetime.now()
continue_reading = True

NULL_CHAR = chr(0)
def writetofile(scr):
    f = open("logs.txt", "a")
    tim = now.strftime("%Y-%m-%d %H:%M:%S")
    if scr == "error":
        f.write("Got error at ")
        f.write(tim)
        f.write("\r\n")
    else:
        f.write("Got Success with ")
        f.write(scr)
        f.write(" at ")
        f.write(tim)
        f.write("\r\n")

# function to send the data
def write_report(report):
    with open('/dev/hidg0', 'rb+') as fd:
        fd.write(report.encode())
def login():
    ##writes my password, commands removed for stack :)


def uidToString(uid):
    mystring = ""
    for i in uid:
        mystring = format(i, '02X') + mystring
    return mystring


def end_read(signal, frame):
    global continue_reading
    print("Ctrl+C captured, ending read.")
    continue_reading = False
    GPIO.cleanup()


signal.signal(signal.SIGINT, end_read)


MIFAREReader = MFRC522.MFRC522()


while continue_reading:

    # Scan for cards
    (status, TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print ("Card detected")
                # Get the UID of the card
        (status, uid) = MIFAREReader.MFRC522_SelectTagSN()
        # If we have the UID, continue
        if status == MIFAREReader.MI_OK:
            carduid = uidToString(uid)
            if carduid == '75AC5685':
                print("KeyRing")
                writetofile("Keyring")
                login()
            elif carduid == 'D539F1FD':
                print("Keycard")
                writetofile("Keycard")
                login()
            print("Card read UID: %s" % carduid)
        else:
            print("Authentication error")
            writetofile("error")

谢谢!!

【问题讨论】:

  • 您是否从它应该运行的时间得到任何日志?你知道它是启动但崩溃了,还是永远不启动?
  • 当您尝试在启动时启动它时,您是用python script.py 还是/usr/bin/python script.py 调用它?
  • @lxop 我没有得到任何日志,我知道它必须以 root 开头,并且 AFAIK 有一个活动进程......不知道为什么,可能与 SPI 有关。
  • @lxop ````python script.py```
  • 尝试指定 python 的完整路径 - 在启动过程中可能会出现 PATH 未按预期设置的问题

标签: python raspberry-pi rfid


【解决方案1】:

感谢@lxop 的帮助,不过这不是我的问题。

很抱歉浪费了帖子,我的错误是我使用 ~ 目录来引用文件,显然没有登录。现在一切正常。不过还是谢谢!

【讨论】:

    猜你喜欢
    • 2021-02-26
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多