[英]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.