2017-06-19 44 views
1

我有以下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

有沒有一種方法,使工作?

先謝謝大俠。

回答

0

爲了讓您的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' 
+1

我結束了做這個: 'ExecStart =/bin中/ bash -c'set -a && source custom_script.sh && start_script.sh'' 非常感謝。 –

+0

嗯,是的,這是一個好主意,只是來源和去。 – pneumatics