Search code examples
python-3.xlinuxcronraspberry-pisystemd

Systemd script not running python script with config.ini


I have an error when creating a systemd service I don't understand.

I run a python script for air quality on an rpi. I runs it using the following code:

python3 senddata.py "./config.ini"

This run correctly; however, I want to make it a systemd service to later setup a cron job. I drafted this up:

[Unit]
Description=aqi
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/.../senddata.py "./config.ini"

[Install]
WantedBy=multi-user.target

When I run it I reload the daemon and restart the service, but get the following error:

python3[...]: KeyError parsing config.ini file.

Is it not understanding my quotes for the config.ini when attempting to run?

Thanks!

Resources: https://ayeks.de/post/2018-05-29-bme680-influxdb-grafana/ https://github.com/ayeks/bme680_to_influxdb/blob/master/grafana_dashboard.json


Solution

  • Out of curiosity - I made a custom service with just 'ls' - to see what directory was the current one when starting a service with systemctl.

    The output in the system journal leads me to believe it's starting with "/" as the current directory. I executed 'systemctl start test' from /etc/systemd/system - not in "/"..

    [Unit]
    Description=aqi
    After=multi-user.target
    
    [Service]
    Type=simple
    ExecStart=/usr/bin/ls
    
    [Install]
    WantedBy=multi-user.target
    
    
    
    Oct 30 13:11:40 oelinux2 ls[1951]: bin
    Oct 30 13:11:40 oelinux2 ls[1951]: boot
    Oct 30 13:11:40 oelinux2 ls[1951]: dev
    Oct 30 13:11:40 oelinux2 ls[1951]: etc
    Oct 30 13:11:40 oelinux2 ls[1951]: home
    Oct 30 13:11:40 oelinux2 ls[1951]: lib
    Oct 30 13:11:40 oelinux2 ls[1951]: lib64
    Oct 30 13:11:40 oelinux2 ls[1951]: media
    Oct 30 13:11:40 oelinux2 ls[1951]: mnt
    Oct 30 13:11:40 oelinux2 ls[1951]: opt
    Oct 30 13:11:40 oelinux2 ls[1951]: proc
    Oct 30 13:11:40 oelinux2 ls[1951]: root
    Oct 30 13:11:40 oelinux2 ls[1951]: run
    Oct 30 13:11:40 oelinux2 ls[1951]: sbin
    Oct 30 13:11:40 oelinux2 ls[1951]: srv
    Oct 30 13:11:40 oelinux2 ls[1951]: sys
    Oct 30 13:11:40 oelinux2 ls[1951]: tmp
    Oct 30 13:11:40 oelinux2 ls[1951]: usr
    Oct 30 13:11:40 oelinux2 ls[1951]: var
    

    ls command on same machine from /

    [root@oelinux2 /]# ls
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    [root@oelinux2 /]#
    

    My first thought.. is to change "./config" to the full path of where 'config' is located. Try that and see if it can find it.