iva*_*ncz 3 bash environment-variables systemd systemd-unit
我有以下单元文件:
[Unit]
Description=Panel for Systemd Services
After=network.target
[Service]
User=pysd
Group=pysd
PermissionsStartOnly=true
WorkingDirectory=/opt/pysd
ExecStartPre=/bin/mkdir /run/pysd
ExecStartPre=/bin/chown -R pysd:pysd /run/pysd
ExecStart=/usr/local/bin/gunicorn app:app -b 127.0.0.1:8100 --pid /run/pysd/pysd.pid --workers=2
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
ExecStopPost=/bin/rm -rf /run/pysd
PIDFile=/run/pysd/pysd.pid
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Alias=pysd.service
Run Code Online (Sandbox Code Playgroud)
我想创建一个环境变量,ExecStartPre然后将此变量合并到ExecStart.
更具体地说,我想GUNICORN_SERVER在运行 之前创建一个环境变量,ExecStart然后将此环境变量用于选项-bat ExecStart。
我尝试了类似的东西ExecStartPre=/bin/bash -c 'export GUNICORN_SERVER=127.0.0.1:8100',但没有创建环境变量。
我如何实现这个场景?
您不能ExecStartPre直接为其他ExecStartPre或ExecStart命令设置环境——这些都是独立的进程。(间接地,通过保存到文件并阅读它或其他东西,当然。)
Systemd 有两种设置环境的方法:Environment=和EnvironmentFile=. 中有两者的例子man 5 systemd.exec。这些会影响服务启动的所有进程,包括ExecStartPre. 如果这些变量不必动态设置,那么这些是一个不错的选择:
Environment=GUNICORN_SERVER=127.0.0.1:8080
Run Code Online (Sandbox Code Playgroud)
但是,如果您需要动态设置变量,联机帮助页说明了以下内容EnvironmentFile:
Environment=GUNICORN_SERVER=127.0.0.1:8080
Run Code Online (Sandbox Code Playgroud)
因此,一种选择是将其写入 中的文件ExecStartPre,并让 systemd 读取该文件作为 的一部分EnvironmentFile:
EnvironmentFile=/some/env/file
ExecStartPre=/bin/bash -c 'echo foo=bar > /some/env/file'
ExecStart=/some/command # sees bar as value of $foo
Run Code Online (Sandbox Code Playgroud)
另一种选择是在以下位置使用外壳ExecStart:
ExecStart=/bin/sh -c 'export GUNICORN_SERVER=127.0.0.1:8080; exec /usr/local/bin/gunicorn ...'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5667 次 |
| 最近记录: |