[英]Systemd service does not write python script to file, python script runs nominally when executed from cli
Working with a raspberry pi zero w, setup a script for monitoring a bme280 sensor with values written to a file. 使用覆盆子pi零w,设置一个脚本,用于监视bme280传感器,并将值写入文件。 This works great when the script is started from the command line, when the script is started via systemd the file is not written. 当从命令行启动脚本时,这非常有效,当通过systemd启动脚本时,不会写入文件。 Please find below the script and systemd service. 请在下面找到脚本和systemd服务。
Have set the Standard output to an absolute path with no luck, the python script write directive is set to absolute path as well. 已将标准输出设置为绝对路径而没有运气,python脚本写入指令也设置为绝对路径。
Systemd service: 系统服务:
[Unit]
Description=bme280 sensor log startup
After=network.target
[Service]
ExecStart=/usr/bin/python3 -u /home/pi/bme.py
WorkingDirectory=/home/pi
StandardOutput=file:/home/pi/senselog.csv
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
python script: python脚本:
import time
from time import strftime
import board
import busio
import adafruit_bme280
# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c,address=0x76)
# OR create library object using our Bus SPI port
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
#bme_cs = digitalio.DigitalInOut(board.D10)
#bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)
# change this to match the location's pressure (hPa) at sea level
bme280.sea_level_pressure = 1013.25
with open("/home/pi/senselog.csv", "a") as log:
while True:
temp_h = bme280.temperature
humidity = bme280.humidity
pressure = bme280.pressure
log.write("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str([temp_h,humidity,pressure])))
time.sleep(60)
If I delete the senselog.csv file, then on boot the systemd service creates the file fresh but with no data, any assistance is appreciated. 如果我删除了senselog.csv文件,那么在启动时,systemd服务会创建文件,但没有数据,任何帮助都表示赞赏。
The file
attribute to StandardOutput
only became available with systemd
version 236. What version do you have? 只有systemd
版本236才能使用StandardOutput
的file
属性。您有什么版本?
pi@wifi-relay:~ $ systemd --version
systemd 232
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN
If it's less than version 236 and you can't/don't want to upgrade, you could simply update your ExecStart
line to: 如果它低于版本236并且您不能/不想升级,您只需将ExecStart
行更新为:
/usr/bin/python3 -u /home/pi/bme.py >1 /home/pi/senselog.csv
...then put back the StandardOutput
line back to the default. ...然后将StandardOutput
线恢复为默认值。
So the solution is to actually call .close() on the file we are writing to in the python script, then the systemd service works as expected. 因此,解决方案是在python脚本中实际调用我们正在写入的文件上的.close(),然后systemd服务按预期工作。 Shout out to this thread: https://askubuntu.com/questions/83549/python-script-wont-write-data-when-ran-from-cron very last answer = f.close() 大声呼吁这个帖子: https ://askubuntu.com/questions/83549/python-script-wont-write-data-when-ran-from-cron最后一个答案= f.close()
and the working script file: 和工作脚本文件:
from time import strftime
import board
import busio
import adafruit_bme280
# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c,address=0x76)
# OR create library object using our Bus SPI port
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
#bme_cs = digitalio.DigitalInOut(board.D10)
#bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)
# change this to match the location's pressure (hPa) at sea level
bme280.sea_level_pressure = 1013.25
with open("/home/pi/senselog.csv", "w") as log:
while True:
temp_h = bme280.temperature
humidity = bme280.humidity
pressure = bme280.pressure
log.write("{0},{1}\n".format(strftime("%Y-%m-%d %H:%M:%S"),str([temp_h,humidity,pressure])))
log.close()
time.sleep(60) ```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.