Linux 可执行脚本中的systemd env vars

Linux 可执行脚本中的systemd env vars,linux,environment-variables,systemd,Linux,Environment Variables,Systemd,我有以下systemd服务文件: [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

我有以下systemd服务文件:

[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参数,但没有成功:

  • 执行程序
  • 环境文件
有没有办法让它发挥作用


提前感谢各位英雄。

为了从Java进程中访问
custom_script.sh
中的变量,您必须以某种方式将它们插入到环境中,以systemd会满意的方式。假设任何不是带有
=
符号的参数赋值语句的行都将被忽略。因此,我们需要将您的脚本编写下来,以便在运行它之后剩下的只是变量

您可以做的是创建一个辅助的“蒸馏器”服务,该服务可以为您的
custom\u script.sh
文件提供来源,并将环境中的每个值打印到另一个名为
custom\u script.env
的文件中。然后,您可以在
EnvironmentFile
指令中向Java进程提供“提取”的环境文件

因此,如果您的原始服务在=之后添加
,并且
要求=
如下所示

[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\u script.sh&&start\u script.sh'
非常感谢。啊,是的,这是一个好主意,只需编写源代码就可以了。