我在一些文件中设置环境变量,/etc/profile.d/mercure.sh
如下所示:
export MERCURE_PUBLISHER_JWT_KEY=$(cat /home/stephane/mercure/publisher.key.pub)
export MERCURE_PUBLISHER_JWT_ALG=RS256
export MERCURE_SUBSCRIBER_JWT_KEY=$(cat /home/stephane/mercure/subscriber.key.pub)
export MERCURE_PUBLISHER_JWT_ALG=RS256
export SERVER_NAME=localhost:3000
我需要它们来运行以下命令(对于美居中心):
MERCURE_PUBLISHER_JWT_KEY=$MERCURE_PUBLISHER_JWT_KEY MERCURE_SUBSCRIBER_JWT_KEY=$MERCURE_SUBSCRIBER_JWT_KEY SERVER_NAME=$SERVER_NAME /usr/bin/mercure run
我想创建一个mercure.service
文件mercure
作为服务运行,如下所述这里
[Unit]
Description=Mercure.Rocks service
After=network.target
StartLimitBurst=5
StartLimitIntervalSec=33
[Service]
Type=simple
WorkingDirectory=/tmp
EnvironmentFile=/etc/profile.d/mercure.sh
ExecStart=MERCURE_PUBLISHER_JWT_KEY=$MERCURE_PUBLISHER_JWT_KEY MERCURE_SUBSCRIBER_JWT_KEY=$MERCURE_SUBSCRIBER_JWT_KEY SERVER_NAME=$SERVER_NAME /usr/bin/mercure run --config /home/stephane/mercure/Caddyfile
StandardOutput=file:/var/log/nginx/mercure.log
StandardError=file:/var/log/nginx/mercure.log
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.targe
一开始,我遇到了这些错误:
$ sudo systemctl status mercure
● mercure.service - Mercure.Rocks service
Loaded: loaded (/etc/systemd/system/mercure.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Wed 2024-01-03 15:26:49 CET; 1s ago
Process: 33752 ExecStart=MERCURE_PUBLISHER_JWT_KEY=$MERCURE_PUBLISHER_JWT_KEY MERCURE_SUBSCRIBER_JWT_KEY=$MERCURE_SUBSCRIBER_JWT_KEY SERVER_NAME=$SERVER_NAME /usr/bin/mercure run --config /home/stephane/mer>
Main PID: 33752 (code=exited, status=203/EXEC)
CPU: 824us
systemd[1]: Stopped Mercure.Rocks service.
systemd[1]: mercure.service: Ignoring invalid environment assignment 'export MERCURE_PUBLISHER_JWT_KEY=$(cat /home/stephane/mercure/publisher.key.pub)': /etc/profile.d/mercure.sh
systemd[1]: mercure.service: Ignoring invalid environment assignment 'export MERCURE_PUBLISHER_JWT_ALG=RS256': /etc/profile.d/mercure.sh
systemd[1]: mercure.service: Ignoring invalid environment assignment 'export MERCURE_SUBSCRIBER_JWT_KEY=$(cat /home/stephane/mercure/subscriber.key.pub)': /etc/profile.d/mercure.sh
systemd[1]: mercure.service: Ignoring invalid environment assignment 'export SERVER_NAME=localhost:3000 #mercure.astro-otter.space': /etc/profile.d/mercure.sh
systemd[1]: Started Mercure.Rocks service.
systemd[1]: mercure.service: Main process exited, code=exited, status=203/EXEC
systemd[1]: mercure.service: Failed with result 'exit-code'.
我真的不明白如何在我的ExecStart
行中使用环境变量,有人可以解释或向我展示一个例子吗?
答案1
尝试一个更简单的环境文件,不带export
.这不是必需的。
ExecStart /bin/bash -c "...."
如果您想使用一些变量,例如$MERCURE_PUBLISHER_JWT_KEY
答案2
在 @frédéric-loyer 和 @AlexP 的回答的帮助下,我设置了环境变量/etc/environment
并编辑了我的mercure.service
文件:
[Unit]
Description=Mercure.Rocks service
After=network.target
StartLimitBurst=5
StartLimitIntervalSec=33
[Service]
Type=simple
WorkingDirectory=/tmp
EnvironmentFile=-/etc/environment
ExecStart=/usr/bin/bash -c "MERCURE_PUBLISHER_JWT_KEY=$MERCURE_PUBLISHER_JWT_KEY MERCURE_SUBSCRIBER_JWT_KEY=$MERCURE_SUBSCRIBER_JWT_KEY SERVER_NAME=$SERVER_NAME /usr/bin/mercure run --config /var/www/mercure/Cad
dyfile"
StandardOutput=file:/var/log/nginx/mercure.log
StandardError=file:/var/log/nginx/mercure.log
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
似乎没问题:
$ sudo systemctl status mercure
● mercure.service - Mercure.Rocks service
Loaded: loaded (/etc/systemd/system/mercure.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-01-04 10:15:04 CET; 3min 52s ago
Main PID: 57957 (mercure)
Tasks: 6 (limit: 2163)
Memory: 8.8M
CPU: 80ms
CGroup: /system.slice/mercure.service
└─57957 /usr/bin/mercure run --config /var/www/mercure/Caddyfile
Jan 04 10:15:04 vps773218 systemd[1]: Started Mercure.Rocks service.
谢谢。