首页 > 解决方案 > Systemd 脚本未使用 config.ini 运行 python 脚本

问题描述

创建我不理解的 systemd 服务时出现错误。

我在 rpi 上运行了一个用于空气质量的 python 脚本。我使用以下代码运行它:

python3 senddata.py "./config.ini"

这运行正确;但是,我想让它成为一个 systemd 服务,以便以后设置一个 cron 作业。我起草了这个:

[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

当我运行它时,我重新加载守护程序并重新启动服务,但收到以下错误:

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

尝试运行时是否不理解我对 config.ini 的引用?

谢谢!

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

标签: python-3.xlinuxcronraspberry-pisystemd

解决方案


出于好奇——我只用“ls”创建了一个自定义服务——在使用 systemctl 启动服务时查看当前目录是什么目录。

系统日志中的输出让我相信它以“/”开头作为当前目录。我从 /etc/systemd/system 执行了“systemctl start test”——不在“/”中。

[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 命令

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

我的第一个想法..是将“./config”更改为“config”所在位置的完整路径。试试看它是否能找到它。


推荐阅读