0

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.

9
  • Should it be file:///home/pi/senselog.csv instead of file:/home/pi/senselog.csv, anyway that is not the issue, i think Commented Mar 26, 2019 at 16:33
  • Could you check and see journalctl -xe -f -u <your_service_name> ? Commented Mar 26, 2019 at 16:35
  • @hansolo here is the output of journalctl: ``` journalctl -xe -f -u bme.service -- Logs begin at Thu 2016-11-03 10:16:44 PDT. -- Mar 26 10:08:39 raspberrypi systemd[1]: Started bme280 sensor log startup. -- Subject: Unit bme.service has finished start-up -- Defined-By: systemd -- Support: debian.org/support -- -- Unit bme.service has finished starting up. -- -- The start-up result is done. ``` Commented Mar 26, 2019 at 17:16
  • Hey, now only i noticed, you have not defined the Type of the service. Considering your script, could you try Type=simple in the [Service] section ? Commented Mar 26, 2019 at 17:20
  • Added Type=simple to the service with no change in behavior.. Commented Mar 26, 2019 at 17:26

2 Answers 2

1

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) ```
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Thanks for the heads up, the pi is on version 233 so I edited the service as you directed with no change in file writing behavior. Other thoughts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.