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

How do I assign the result of curl to a systemd variable?

· 52CE 我爱测

I am using an Amazon Web Service server, and am trying to setup a systemd job that takes as an input the AWS external IP retrieved by curl . [Unit] Description=My example with AWS external IP Wants=network-online.target After=net…

问题原文

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

I am using an Amazon Web Service server, and am trying to setup a systemd job that takes as an input the AWS external IP retrieved by curl.

[Unit]
Description=My example with AWS external IP
Wants=network-online.target
After=network-online.target

[Service]
Type=forking
Environment=EXTERNAL_IP=$(/usr/bin/curl http://169.254.169.254/latest/meta-data/public-ipv4)
ExecStart=/path/to/myexample $EXTERNAL_IP

[Install]
WantedBy=multi-user.target

The verify tool systemd-analyze verify myexample.service indicates there is a problem with curl and the environment assignment.

Invalid environment assignment, ignoring: EXTERNAL_IP=$(/usr/bin/curl http://169.254.169.254/latest/meta-data/public-ipv4)

If I replace the curl result with a hard-coded IP string, my daemon starts. How can I use the curl result in my systemd job?

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 3.0

(First, I presume you are using coturn or one of its predecessors.)

To begin with, systemd units are not shell scripts and aren't parsed by a shell. This is why you can't just call a command and use its output as an environment variable.

However, you can call a shell from ExecStart=, which might be the fastest way to get up and running.

Type=simple
ExecStart=/bin/bash -c '/usr/local/bin/turnserver -X $(/usr/bin/curl http://169.254.169.254/latest/meta-data/public-ipv4)'

(In this configuration you should not daemonize turnserver with -o.)

You could also write a script, which you can call from ExecStartPre=, which fetches your metadata and then alters turnserver.conf, which would occur before the daemon is started.

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

资料来源与适用范围

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

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

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