简体   繁体   English

Linux / Raspberry Pi OS - 设置将与 systemd 服务一起使用的环境变量

[英]Linux / Raspberry Pi OS - Setting environment variable that will work with a systemd service

I asked this question yesterday which in essence is regarding running a python3 script (that uses an environment variable set in etc/enviroment ) that is run by a systemd service which I've named mqtt.service .昨天问了这个问题,本质上是关于运行由我命名为mqtt.service的 systemd 服务运行的 python3 脚本(使用在etc/enviroment中设置的环境变量)。

I got some useful information from an answer to that question indicating that by default environment variables in /etc/environment aren't accessible in systemd , and that answer linked to this question that details the steps you can take to allow the systemd service to see an enviroment variable of your choosing.我从该问题的答案中获得了一些有用的信息,表明默认情况下/etc/environment中的环境变量在systemd中不可访问,并且该答案链接到该问题,详细说明了您可以采取哪些步骤来允许 systemd 服务查看您选择的环境变量。

So far I've tried the following:到目前为止,我已经尝试了以下方法:

Within my service I've tried to add EnvironmentFile=/etc/environment which contains my environment variable: export DEVICE_ID="TEST1" .在我的服务中,我尝试添加EnvironmentFile=/etc/environment ,其中包含我的环境变量: export DEVICE_ID="TEST1" I've also tried using the systemctl edit mqtt.service to create mqtt.service.d/override.conf file that also has EnvironmentFile=/etc/environment set.我还尝试使用systemctl edit mqtt.service创建mqtt.service.d/override.conf文件,该文件也设置了EnvironmentFile=/etc/environment

Next I tried instead to set the Environment=$DEVICE_ID in the mqtt.service directly as well as the mqtt.service.d/override.conf file.接下来我尝试直接在mqtt.service以及mqtt.service.d/override.conf文件中设置Environment=$DEVICE_ID

I then tried all of the above but changing where the $DEVICE_ID environment variable was set.然后我尝试了上述所有方法,但更改了$DEVICE_ID环境变量的设置位置。 So far I've tried setting it in etc/profile with a value of export DEVICE_ID="TEST1" .到目前为止,我已经尝试在etc/profile中将其设置为export DEVICE_ID="TEST1"的值。

Finally I also tried creating a device_id.sh file within /etc/profile.d/device_id.sh which has a value of export DEVICE_ID=TEST1 .最后,我还尝试在/etc/profile.d/device_id.sh中创建一个device_id.sh文件,其值为 export DEVICE_ID=TEST1 Which gave me the following results when i tried to run the mqtt.service :当我尝试运行mqtt.service时,这给了我以下结果:

user@computer:/etc/profile.d $ 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)
    Drop-In: /etc/systemd/system/mqtt.service.d
             └─override.conf
     Active: activating (auto-restart) (Result: exit-code) since Fri 2022-08-12 14:49:52 BST; 4s ago
    Process: 12941 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
   Main PID: 12941 (code=exited, status=1/FAILURE)
        CPU: 551ms

user@computer:/etc/profile.d $ 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)
    Drop-In: /etc/systemd/system/mqtt.service.d
             └─override.conf
     Active: activating (auto-restart) (Result: exit-code) since Fri 2022-08-12 14:49:52 BST; 4s ago
    Process: 12941 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
   Main PID: 12941 (code=exited, status=1/FAILURE)
        CPU: 551ms
user@computer:/etc/profile.d $

Everything I tried above gave me simular error messages when trying to run the mqtt.service .在尝试运行mqtt.service时,我在上面尝试的所有操作都给了我类似的错误消息。

I'm really struggling with this, I don't know If I'm, missing something or is it the case that what I'm trying to achieve isn't possible?我真的在为此苦苦挣扎,我不知道我是否错过了某些东西,或者我想要实现的目标是不可能的?

As explained in the second answer to the link you gave, and man environment.d , the file /etc/environment is used to set variables in the user services environment, ie services started with systemctl --user start... .正如您提供的链接的第二个答案man environment.d中所解释的,文件/etc/environment用于在用户服务环境中设置变量,即使用systemctl --user start...的服务。 You are using system services, so if you want to include this file you needed to set您正在使用系统服务,因此如果要包含此文件,则需要设置

[Service]
EnvironmentFile=/etc/environment

which you probably have by now.你现在可能已经有了。 Make sure it is in the right section.确保它位于正确的部分。 You also need to run sudo systemctl daemon-reload after editing a unit file.您还需要在编辑单元文件后运行sudo systemctl daemon-reload

However, the man page also explains that the format for lines in the environment file is extremely limited.但是,手册页还解释说,环境文件中的行格式非常有限。 In particular, the shell command export is not allowed.特别是 shell 命令export是不允许的。 Although it is not mentioned, double-quotes are allowed, but not needed.虽然没有提到,双引号是允许的,但不是必需的。 So you should try an entry like所以你应该尝试像这样的条目

DEVICE_ID=TEST1

You should do a systemctl --user daemon-reload after editing this file for it to be parsed and checked for errors, but you need to look for the error messages in one of the various system log files.您应该在编辑此文件后执行systemctl --user daemon-reload以对其进行解析和检查错误,但您需要在各种系统日志文件之一中查找错误消息。 You can see any changes with systemctl --user show-environment .您可以使用systemctl --user show-environment查看任何更改。

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

相关问题 Linux / Raspberry Pi OS - Systemd 无法使用访问环境变量的 OS 模块处理 python3 脚本 - Linux / Raspberry Pi OS - Systemd not working with python3 script using OS module that accesses enviroment variable Raspberry Pi 和 linux systemd 和 .system 文件 - Raspberry Pi, and linux systemd, and .system files 尝试使用 systemd 启动 python 脚本,但它在 Raspberry Pi 上不起作用 - Trying to launch a python script with systemd but it doesnt work on Raspberry Pi Python更新Linux(Raspberry Pi) - Python update Linux (Raspberry Pi) IBM Raspberry Pi物联网服务 - IBM Raspberry Pi IoT Service Raspberry Pi 2 OS.System()命令返回0 - Raspberry pi 2 os.System() command returning 0 在树莓派 B+ 上使用 tkinter 从引导 shell 脚本没有显示名称和 $DISPLAY 环境变量 - No display name and no $DISPLAY environment variable using tkinter on raspberry Pi B+ from boot shell script Raspberry Pi GPIO中断回调变量 - Raspberry Pi GPIO Interrupt callback variable 将变量传递给在 Raspberry Pi 上实现的 Dialogflow 聊天机器人 - Passing variable to Dialogflow Chatbot implemented on Raspberry Pi 无法安装 python-fingerprint 模块 o Raspberry Pi OS - Unable to install python-fingerprint module o Raspberry Pi OS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM