在服务文件中的aa环境语句中执行linux命令

在服务文件中的aa环境语句中执行linux命令

我有这个.service文件。我ExecStart需要传递一些我认为可以在环境中设置为变量的参数,但首先我必须从.conf带有 agrep和 a 的文件中获取它们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,但幸运的是环境变量替换是少数受支持的 shell 风格功能之一。如果用作单个单词$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

您可以看到,我们通过 中设置的脚本正确打印了和echo的值。HOSTPORTEnvironmentFileExecStartPre

相关内容