作为systemctl服务运行时,Python脚本未写入配置

作为systemctl服务运行时,Python脚本未写入配置,python,service,raspbian,configuration-files,configparser,Python,Service,Raspbian,Configuration Files,Configparser,我正在raspbian上运行一个python 3程序,该程序必须将字符串值写入配置文件。 我设法在主脚本中写入config,但不在“辅助”脚本中写入 ->在VS代码(远程调试器)中调试时,辅助脚本会正确写入config.txt ->当使用sudo systemctl start myservice或su-c'systemctl start myservice'作为服务运行时,辅助脚本不会写入config.txt 在这两种情况下,程序都会毫无例外地运行到最后 /home/pi/project/my

我正在raspbian上运行一个python 3程序,该程序必须将字符串值写入配置文件。 我设法在主脚本中写入config,但不在“辅助”脚本中写入

->在VS代码(远程调试器)中调试时,辅助脚本会正确写入config.txt

->当使用
sudo systemctl start myservice
su-c'systemctl start myservice'
作为服务运行时,辅助脚本不会写入config.txt

在这两种情况下,程序都会毫无例外地运行到最后

/home/pi/project/my script.py

# This is the main script. 
# If I move the configWrite method in this one, it writes correctly to config.

from lib import *

def main():
    secondary.authenticate()

if __name__ == '__main__':
    main()
# This is the script which can't write to config.

import configparser
import logging
import requests

config = configparser.ConfigParser()   
configFilePath = r'/home/pi/project/config.txt'
config.read(configFilePath)
cfg = config['main']
sid = cfg['sid']

def configWrite(field, value):
    config.set('secondary', field, value)
    with open(configFilePath, 'w') as configfile:
        config.write(configfile)

def authenticate():
    authenticate_url = '...'
    headers = { ... }
    try:
        response = requests.post(authenticate_url, headers=headers, timeout=(30, 10))
        response.raise_for_status()
    except Exception as err:
        logging.warning('Error occurred: {}'.format(err))
        return False
    else:
        global sid
        sid = response.json()['sid']
        configWrite('sid', str(sid))
        return True
/home/pi/project/lib/secondary.py

# This is the main script. 
# If I move the configWrite method in this one, it writes correctly to config.

from lib import *

def main():
    secondary.authenticate()

if __name__ == '__main__':
    main()
# This is the script which can't write to config.

import configparser
import logging
import requests

config = configparser.ConfigParser()   
configFilePath = r'/home/pi/project/config.txt'
config.read(configFilePath)
cfg = config['main']
sid = cfg['sid']

def configWrite(field, value):
    config.set('secondary', field, value)
    with open(configFilePath, 'w') as configfile:
        config.write(configfile)

def authenticate():
    authenticate_url = '...'
    headers = { ... }
    try:
        response = requests.post(authenticate_url, headers=headers, timeout=(30, 10))
        response.raise_for_status()
    except Exception as err:
        logging.warning('Error occurred: {}'.format(err))
        return False
    else:
        global sid
        sid = response.json()['sid']
        configWrite('sid', str(sid))
        return True
/home/pi/project/config.txt(chmoded为666)

/etc/systemd/system/myservice.service

[Unit]
Description=My service description
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u my-script.py
WorkingDirectory=/home/pi/project
StandardOutput=Inherit
StandardError=Inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target
  • 此处有类似问题,但没有有效答案:
  • 通过检查日志,我可以看到服务可以正常运行到脚本末尾(无论哪种方式,日志都可以正常工作)
  • 日志中没有错误
  • 我已经重新启动了覆盆子派
  • Python版本3.5.3

    • 我不确定,但看起来您从未将config['main']设置为新值。
      在configWrite(field,value):config['main']={field:value}中尝试,然后像bevor一样将其写入config.txt。

      粘贴stackoverflow的编辑代码时,该行丢失。我编辑了这个问题。很抱歉,那么我将尝试在ExecStart处为scipt添加绝对路径。这为我解决了问题。像ExecStart=/usr/bin/python3-u/home/pi/project/my-script.pyNope一样,脚本仍然没有写入到configure中。我猜复制时请求导入也丢失了?您是否尝试以root用户身份运行该服务?我编辑了该问题以还原导入请求。我还尝试了su命令(不幸的是,它产生了与sudo命令相同的结果)