Reputation: 124
Working with a raspberry pi zero w, setup a script for monitoring a bme280 sensor with values written to a file. This works great when the script is started from the command line, when the script is started via systemd the file is not written. Please find below the script and systemd service.
Have set the Standard output to an absolute path with no luck, the python script write directive is set to absolute path as well.
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:
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.
Upvotes: 0
Views: 1078
Reputation: 124
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. Shout out to this thread: https://askubuntu.com/questions/83549/python-script-wont-write-data-when-ran-from-cron very last answer = 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) ```
Upvotes: 1
Reputation: 9296
The file
attribute to StandardOutput
only became available with systemd
version 236. What version do you have?
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:
/usr/bin/python3 -u /home/pi/bme.py >1 /home/pi/senselog.csv
...then put back the StandardOutput
line back to the default.
Upvotes: 0