大网数据广告活动
服务器运维问答

Systemd Environment and EnvironmentFile not working

· 52CE 我爱测

I've built an application and a systemd unit for it. The systemd unit works fine, but as the dev and prod environments have diverged I've started moving config out to environment variables and I can't seem to get them to work in …

问题原文

原帖链接 · Keef Baker · CC BY-SA 3.0

I've built an application and a systemd unit for it. The systemd unit works fine, but as the dev and prod environments have diverged I've started moving config out to environment variables and I can't seem to get them to work in systemd.

I've tried system-wide environment variables, and they're visible to the OS, but not the program, so i started looking at building them into systemd units.

First I tried using EnvironmentFile

I created an environment file that simply had

LCSQLH=localhost
LCSQLU=application

in it as /etc/lc.sh and

[Unit]
Description=Service for this app

[Service]
EnvironmentFile=/etc/lc.sh
ExecStart=/usr/bin/env python /opt/app/__init__.py

did a systemctl --system daemon-reload but no, my app errored:

Jan 27 14:24:59 machine.host env[630]: KeyError: 'LCSQLU'

I saw some had:

EnvironmentFile=-/etc/lc.sh

I tried that.... nope...

So I tried putting them in individually

[Service]
Environment="LCSQLH=localhost"
Environment="LCSQLU=application"
ExecStart=/usr/bin/env python /opt/app/__init__.py

Yet again, no...

So I heard about the idea of /etc/systemd/service_name.service.d so I put a service.conf in there with the environments in (same format as above) but no...

My application has no access to these environment variables.

If I export them (either manually in my shell or using /etc/profile.d/) and run my app directly it works, so it is that these aren't being set rather than an app issue.

This is Centos 7.3 and I've chosen environment variables rather than a hardcoded config because it may run on either linux or windows, so don't want to bury a config file in /etc/

已采纳解答

原帖链接 · noobish · CC BY-SA 3.0

I ran into the same trouble on RHEL 7.3 and found this:

You may then refer to variables set in the /etc/sysconfig/httpd file with ${FOOBAR} and $FOOBAR, in the ExecStart lines (and related lines).

This makes me think that the purpose for Environment and EnvironmentFile is not at all what you and I expected (setting environment variables for the process started by the systemd unit), but only applies for the immediate expansion of the ExecStart line.

Perhaps I'm completely off-base and this is a bug in systemd (I hope so). But I stopped going down the path you are trying and did it another way: In my case I needed to set LD_LIBRARY_PATH, so I did that via creating /etc/ld.so.conf.d/new_file.conf and then running ldconfig. I also attempted to use system-wide variables in /etc/profile.d/new_file.sh, but apparently setting just LD_LIBRARY_PATH was enough for this service (mariadb) and so I don't actually know if the variables I was setting in /etc/profile.d/new_file.sh were functioning.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-01-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。