systemd ENV从可执行脚本瓦尔
问题描述:
我有以下systemd服务文件:systemd ENV从可执行脚本瓦尔
[Unit]
Description=My description
[Service]
Type=simple
User=myuser
ExecStart=/path/to/my/start_script.sh
ExecStop=/path/to/my/stop_script.sh
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
我start_script.sh用来启动Java应用程序,但我需要从一个可执行得到一些变数ksh脚本custom_script.sh。
我尝试没有成功以下systemd PARAMS:
- ExecStartPre
- EnvironmentFile
有没有一种方法,使工作?
先谢谢大侠。
答
为了让您的Java进程可以访问custom_script.sh
的变量,您必须以某种方式将它们插入到环境中,以systemd将会感到满意的方式。 docs for the EnvironmentFile=
directive表示任何不是带有=
符号的参数赋值语句的行都将被忽略。所以我们需要把脚本写下来,让我们剩下的就是运行后的变量。
你可以做的是创建一个辅助的“酒厂”服务,该服务源自你的custom_script.sh
文件,并将环境中的每个值打印到另一个名为custom_script.env
的文件中。然后,您可以在EnvironmentFile
指令中向Java进程提供“蒸馏”环境文件。
所以,如果你原来的服务添加After=
和Requires=
这样,
[Unit]
Description=My description
After=custom-script-distillery
Requires=custom-script-distillery
[Service]
Type=simple
User=myuser
EnvironmentFile=/path/to/my/custom_script.env
ExecStart=/path/to/my/start_script.sh
ExecStop=/path/to/my/stop_script.sh
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
然后酒厂看起来是这样的:
[Unit]
Description=My service to distill custom_script.sh to an EnvironmentFile
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c 'set -o allexport; source /path/to/my/custom_script.sh; set +o allexport; unset IFS; set | grep -v "^BASH" > /path/to/my/custom_script.env'
我结束了做这个: 'ExecStart =/bin中/ bash -c'set -a && source custom_script.sh && start_script.sh'' 非常感谢。 –
嗯,是的,这是一个好主意,只是来源和去。 – pneumatics