サービスファイルのaa環境文でLinuxコマンドを実行します。

サービスファイルのaa環境文でLinuxコマンドを実行します。

このファイルがあります.serviceExecStart環境で変数に設定できると考えられるいくつかのパラメータを渡す必要があります。ただし、最初にaとを含むファイルから対応するパラメータを.conf取得するgrep必要がありますcut

ただし、ファイルから値を取得できるように環境でLinuxステートメントを実行できることを知っておく必要があります.conf

Environment=(here would be the code for the variables)
ExecStart=(here i would use them)

ベストアンサー1

既存の項目を変更しEnvironmentFileExecStartPre以下から適用できますExecStart

~/.config/systemd/user/envtest.service:

[Unit]
Desciption=Test EnvironmentFile usage

[Service]
Type=oneshot
#
# An environment file can be used to set a bunch of variables 
# %t is a runtime directory, The location is arbitary so use what
# works for you.
EnvironmentFile=%t/envtest.environment
#
# We will edit the environment file from this bash script.
# The file itself is passed as an argument into the script.
# This line will fail to start if the EnvironmentFile doesn't
# exist yet. So it might be better to have a permanent file 
# somewhere such as `/var/lib`. 
ExecStartPre=%h/bin/create_environment.sh %t/envtest.environment
#
# The environment file will be re-read and applied to the service
# I am using simply using `env` to print out the current environment
# so we can see whether the contents of our EnvironmentFile are used
ExecStart=/usr/bin/env

~/bin/create_environment.sh:

#!/bin/bash

# Add your `grep *.conf | cut > $1` here
# This variable is for demonstration
echo "TestVariable=Added" > $1

EnvironmentFile=このコマンドを実行すると、コマンドが存在しない場合に失敗することを示します。ただし、最初にファイルを作成したら(おそらく起動の間/varに持続する場所にファイルを配置したい場合)、サービスを開始できます。

$ systemctl --user start envtest.service
...
Feb 12 14:47:59 stewbian systemd[992]: envtest.service: Failed to load environment files: No such file or directory
Feb 12 14:47:59 stewbian systemd[992]: envtest.service: Failed to run 'start-pre' task: No such file or directory

$ touch /run/user/$UID/envtest.environment
$ systemctl --user start envtest.service
$ journalctl --user -u evntest.service | grep TestVariable
Feb 12 14:49:10 stewbian envtest[261219]: TestVariable=Added

journalctlサービスによって印刷された出力でこれを確認できますTestVariable=Added。これは、ファイルに対する変更がEnvironmentVariableサービス環境に現れることを意味します。ExecStartPreExecStart

唯一注目すべき点は、EnvironmentFileサービスが実行される前に存在する必要があることです(したがって、おそらく私のように%T//tmpまたは/を使用しないでください)。%t/run


OPのコメントに基づいて更新:で変数を設定すると、EnvironmentVariableアプリケーションが環境変数を読み取ろうとすると、環境変数にアクセスできます。環境のインポート(3))。あなたのコメントでは、次のようにコマンドライン呼び出しでも変数を使用することを提案しました。

ExecStart=/path/to/bin/app --host=$HOST --port=$PORT

bash人々はしばしばに構文を追加する間違いを犯しますが、ExecStart幸いにも環境変数の交換サポートされている数少ないシェルスタイル機能の1つです。単一の単語として使用すると$PORT置き換えられます。上記の例では、${PORT}変数が単語の一部であってもより明示的に使用する必要があります。デモは次のとおりです。

$ cat ~/bin/create_environment.sh
#!/bin/bash
echo "HOST=127.0.0.1" > $1
echo "PORT=11111" >> $1

$ systemctl --user cat envtest.service
# /home/stew/.config/systemd/user/envtest.service
[Service]
Type=oneshot
EnvironmentFile=%t/envtest.environment
ExecStartPre=%h/bin/create_environment.sh %t/envtest.environment
ExecStart=/bin/echo --host=${HOST} --port=${PORT}

$ touch /run/user/$UID/envtest.environment
$ systemctl --user start envtest.service
$ journalctl --user -u envtest.service | grep echo
Feb 12 21:28:00 stewbian echo[268399]: --host=127.0.0.1 --port=11111

で設定したスクリプトによって、 の値が正しく印刷されることを確認できますechoHOSTPORTEnvironmentFileExecStartPre

おすすめ記事