如何在.service脚本中使用环境变量

Rob*_*ion 3 linux bash elixir systemd

我在Linux服务器上有一个elixir项目。我创建了一个.service脚本,该脚本将使用以下命令启动和停止应用程序

systemctl start "my_app".service
systemctl stop "my_app".service
Run Code Online (Sandbox Code Playgroud)

.service文件看起来像这样...

[Unit]
Description="example app_name daemon"

[Service]
Type=simple
User=root
Restart=on-failure
Environment=MIX_ENV=prod "PORT=4000"

WorkingDirectory="the file path to my app"

ExecStart=/usr/local/bin/mix phoenix.server

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

它可以按预期工作,唯一的例外是它不会读取环境变量。我可以像PORT=4000上面一样手动将它们添加到脚本中,从而使应用程序可以访问它们。

我想知道.service文件是否可以访问我在服务器上拥有的环境变量,因此我不必在新服务器上使用此脚本的每次都写它们。提前致谢

如果您想要有关该项目的更多信息,请随时查看该仓库

iam*_*ser 5

您正在本节中寻找EnvironmentFile=指令[Service]

EnvironmentFile=/path/to/some/textFile
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多信息。