有没有一种方法可以使用systemd在 Boot 时使用SQLite运行Raspberry Pi Python代码?

wyyhbhjk  于 2023-06-07  发布在  Python
关注(0)|答案(1)|浏览(149)

我正在设计一个代码,它使用Raspberry Pi在 Boot 时将数据附加到SQLite数据库。当我最初使用txt文件时,systemd服务工作得很好,但是现在我为了健壮性而转移到使用SQLite,该服务在 Boot 时无法正常工作。我已经用txt文件和SQLite数据库多次测试了这个设置。它在 Boot 时使用txt文件工作,但不使用SQLite数据库。当正常运行代码时(不是在 Boot 期间),txt文件和SQLite数据库都工作正常。
我已经尝试更改我的systemd设置,以便在 Boot 过程中稍后启动代码(即,type=idle或After=graphical.target),但SQLite数据库仍然无法在启动时运行。对如何处理这个问题有什么建议吗?
Boot 时使用的服务如下:VoltCurr.service

[Unit]
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/wrhsc1310/Python/VoltCurrWrite.py
[Install]
WantedBy=multi-user.target

用于记录电压和电流数据的Python代码如下:

import time
import sqlite3
import Adafruit_ADS1x15
import board
from adafruit_ina219 import ADCResolution, BusVoltageRange, INA219
i2c_bus = board.I2C()  # uses board.SCL and board.SDA
# i2c_bus = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller

ina219 = INA219(i2c_bus)

# optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage
ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S
ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S
# optional : change voltage range to 16V
ina219.bus_voltage_range = BusVoltageRange.RANGE_16V

# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()

# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
#  - 2/3 = +/-6.144V
#  -   1 = +/-4.096V
#  -   2 = +/-2.048V
#  -   4 = +/-1.024V
#  -   8 = +/-0.512V
#  -  16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 2

Time_Const=0
elapsed_time=0
i=0

while True:
    Time_Const=Time_Const+elapsed_time
    start_time=time.time()
    Time = (Time_Const) #Time in seconds
    Current = (ina219.current) #Current in mA
    Voltage = (adc.read_adc(0, gain=GAIN))*(0.0000625) #Voltage in Volts
    connection = sqlite3.connect('PowerData.db')
    cursor = connection.cursor()
    cursor.execute("INSERT INTO Data (Time, Voltage, Current) VALUES (?, ?, ?)",[Time,Voltage,Current])
    connection.commit()
    cursor.close()
    connection.close()
    i=i+1
    time.sleep(0.01)
    end_time=time.time()
    elapsed_time=end_time-start_time

服务错误消息:

● VoltCurr.service
     Loaded: loaded (/lib/systemd/system/VoltCurr.service; enabled; vendor p>
     Active: failed (Result: exit-code) since Tue 2023-05-30 19:04:48 BST; 2>
    Process: 435 ExecStart=/usr/bin/python /home/wrhsc1310/Python/VoltCurrWr>
   Main PID: 435 (code=exited, status=1/FAILURE)
        CPU: 1.951s

May 30 19:04:36 raspberrypi systemd[1]: Started VoltCurr.service.
May 30 19:04:47 raspberrypi python[435]: Traceback (most recent call last):
May 30 19:04:47 raspberrypi python[435]:   File "/home/wrhsc1310/Python/VoltCurrWrite.py", line 43, in <module>
May 30 19:04:47 raspberrypi python[435]:     cursor.execute("INSERT INTO Data (Time, Voltage, Current) VALUES (?, ?, ?)",[Time,Voltage,Current])
May 30 19:04:47 raspberrypi python[435]: sqlite3.OperationalError: no such table: Data
May 30 19:04:48 raspberrypi systemd[1]: VoltCurr.service: Main process exited, code=exited, status=1/FAILURE
May 30 19:04:48 raspberrypi systemd[1]: VoltCurr.service: Failed with result 'exit-code'.
May 30 19:04:48 raspberrypi systemd[1]: VoltCurr.service: Consumed 1.951s CPU time.
83qze16e

83qze16e1#

您正在尝试从脚本的当前工作目录打开数据库:

connection = sqlite3.connect('PowerData.db')

由于您没有明确说明该文件的位置,这意味着当通过systemd运行时,默认工作目录是/,您试图打开/PowerData.db,而该文件可能不存在。
您需要在代码中使用文件的完整路径,或者在systemd单元文件中显式设置WorkingDirectory
如果从系统位置运行systemd单元(如/etc/systemd/system),您可能还需要设置User

相关问题