简体   繁体   English

Linux / Raspberry Pi OS - Systemd 无法使用访问环境变量的 OS 模块处理 python3 脚本

[英]Linux / Raspberry Pi OS - Systemd not working with python3 script using OS module that accesses enviroment variable

I'm trying to use systemd to run a python3 script, it was working fine, however I changed my python script to use the built in OS module as I wanted to retrieve an enviroment variable from the system to use in the python script as a variable.我正在尝试使用 systemd 运行 python3 脚本,它工作正常,但是我更改了我的 python 脚本以使用内置的 OS 模块,因为我想从系统中检索环境变量以在 python 脚本中使用多变的。

The python script is as follows: python 脚本如下:

#/usr/bin/python

import sys, requests, json, time, os
import paho.mqtt.client as mqtt
from requests.auth import HTTPBasicAuth
from datetime import datetime

MQTT_DEVICE_ID = os.environ['DEVICE_ID']+"/DEFENCE"

The DEVICE_ID enviroment variable comes from one I set in /etc/enviroment : DEVICE_ID环境变量来自我在/etc/enviroment中设置的变量:

export DEVICE_ID="TEST1"

user@computer:~ $ echo $DEVICE_ID
TEST1

After adding this change to my python script the systemd service that runs this script no longer starts, it will work for a brief period then it will keep failing and thren working again and failing:将此更改添加到我的 python 脚本后,运行此脚本的 systemd 服务不再启动,它将工作一小段时间,然后继续失败,然后再次工作并失败:

computer@computer:~ $ sudo systemctl status mqtt.service
● mqtt_defense.service - Arms the mqtt.py script that will alert if the device is moved
     Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Thu 2022-08-11 08:57:37 BST; 1s ago
    Process: 2442 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
   Main PID: 2442 (code=exited, status=1/FAILURE)
        CPU: 631ms
    
Aug 11 08:57:37 computer systemd[1]: mqtt.service: Main process exited, code=exited, status=1/FAILURE
Aug 11 08:57:37 computer systemd[1]: mqtt.service: Failed with result 'exit-code'.
user@computer:~ $ sudo systemctl status mqtt_defense.service
● mqtt.service - Arms the mqtt.py script that will alert if the device is moved
     Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Thu 2022-08-11 08:57:37 BST; 3s ago
    Process: 2442 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
   Main PID: 2442 (code=exited, status=1/FAILURE)
        CPU: 631ms

Aug 11 08:57:37 computer systemd[1]: mqtt.service: Main process exited, code=exited, status=1/FAILURE
Aug 11 08:57:37 computer systemd[1]: mqtt.service: Failed with result 'exit-code'.
user@computer:~ $ sudo systemctl status mqtt.service
● mqtt.service - Arms the mqtt_defense.py script that will alert if the device is moved
     Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-08-11 08:57:59 BST; 304ms ago
   Main PID: 2475 (python3)
      Tasks: 1 (limit: 3720)
        CPU: 295ms
     CGroup: /system.slice/mqtt.service
             └─2475 python3 /scripts/mqtt.py

Aug 11 08:57:59 computer systemd[1]: Started Arms the mqtt.py script that will alert if the device is moved.
user@computer:~ $ sudo systemctl status mqtt.service
● mqtt.service - Arms the mqtt.py script that will alert if the device is moved
     Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Thu 2022-08-11 08:58:40 BST; 2s ago
    Process: 2551 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
   Main PID: 2551 (code=exited, status=1/FAILURE)
        CPU: 633ms

After reading a few other questions on this issue such as this , I've tried altering my service to add the WorkingDirectory , User and Group as follows:在阅读了有关此问题的其他一些问题(例如this )后,我尝试更改我的服务以添加WorkingDirectoryUserGroup ,如下所示:

[Unit]
Description=Runs the mqtt.py script 
After=multi-user.target

[Service]
WorkingDirectory=/scripts/
User=user
Group=user
Type=simple
ExecStart=python3 /scripts/mqtt.py
Restart=always
RestartSec=5
TimeoutSec=60
RuntimeMaxSec=infinity

[Install]
WantedBy=multi-user.target

I've also tried changing the User and Group to root, still with no success.我也尝试将UserGroup更改为root,但仍然没有成功。 If I don't use the OS module in my python script the systemd service runs perfectly.如果我不在 python 脚本中使用 OS 模块,systemd 服务将完美运行。

I have a feeling this is a specific problem with the python OS module or that I'm trying to access etc/enviroment in my python script, but I'm not sure what would be the issue.我觉得这是 python 操作系统模块的一个特定问题,或者我试图在我的 python 脚本中访问etc/enviroment ,但我不确定会是什么问题。

Any help would be appreciated, thanks.任何帮助将不胜感激,谢谢。

This has nothing to do with Python.这与 Python 无关。 Your problem is that systemd doesn't expose the env vars defined in /etc/environment by default.您的问题是 systemd 默认不公开/etc/environment中定义的环境变量。 See, for example, https://unix.stackexchange.com/questions/473001/env-vars-in-etc-environment-not-globally-visible .例如,参见https://unix.stackexchange.com/questions/473001/env-vars-in-etc-environment-not-globally-visible As explained in the answer to that question /etc/environment is only loaded by PAM (pluggable authentication module) managed sessions such as interactive logins.正如该问题的答案中所解释的那样, /etc/environment仅由 PAM(可插入身份验证模块)管理的会话(例如交互式登录)加载。 AFAIK, systemd doesn't use PAM. AFAIK,systemd 不使用 PAM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM