在命令路径中为Systemd服务中的ExecStart使用变量

错误的:

我正在尝试制作如下的systemd服务:

[Unit]
Description=Syslog

[Service]
Type=simple
Environment="TESTEXTSERVICESFILES=/opt/test/extservices"
Environment="TESTCONFDATA=/storage/test/conf"

ExecStartPre=/bin/echo ${TESTEXTSERVICESFILES}/syslog/bin/nxlog $TESTCONFDATA
ExecStart=/opt/test/extservices/syslog/bin/nxlog -c ${TESTCONFDATA}/syslog/nxlog.conf
#ExecStart=/${TESTEXTSERVICESFILES}/syslog/bin/nxlog -c ${TESTCONFDATA}/syslog/nxlog.conf


[Install]
WantedBy=multi-user.target

运行sudo systemctl daemon-reload之后;sudo systemctl start test-syslog; sudo systemctl status test-syslog ',我得到以下成功输出:

● test-syslog.service - TestSyslog
   Loaded: loaded (/usr/lib/systemd/system/test-syslog.service; enabled; vendor preset: disabled)
   Active: deactivating (stop-sigterm) since Fri 2018-02-23 10:15:09 UTC; 11ms ago
  Process: 9474 ExecStart=/./opt/test/extservices/test-syslog/bin/nxlog -c ${TESTCONFDATA}/test-syslog/nxlog.conf (code=exited, status=0/SUCCESS)
  Process: 9471 ExecStartPre=/bin/echo /.${TESTEXTSERVICESFILES}/test-syslog/bin/nxlog $TESTCONFDATA (code=exited, status=0/SUCCESS)
 Main PID: 9474 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/test-syslog.service
           └─9478 /./opt/test/extservices/test-syslog/bin/nxlog -c /storage/test/conf/test-syslog/nxlog.conf

Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service: control process exited, code=exited status=0
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service got final SIGCHLD for state start-pre
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: About to execute: /./opt/test/extservices/test-syslog/bin/nxlog -c ${TESTCONFDATA}/test-syslog/nxlog.conf
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: Forked /./opt/test/extservices/test-syslog/bin/nxlog as 9474
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service changed start-pre -> running
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: Job test-syslog.service/start finished, result=done
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: Started Test Syslog.
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: Child 9474 belongs to test-syslog.service
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service: main process exited, code=exited, status=0/SUCCESS
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service changed running -> stop-sigterm

此处服务已成功启动。但是,当我注释第一个ExecStart指令并取消注释第二个ExecStart指令时,我会以失败告终:

● test-syslog.service - Test Syslog
   Loaded: loaded (/usr/lib/systemd/system/test-syslog.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2018-02-23 10:11:44 UTC; 11ms ago
  Process: 9243 ExecStart=/$TESTEXTSERVICESFILES/test-syslog/bin/nxlog -c $TESTCONFDATA/test-syslog/nxlog.conf (code=exited, status=203/EXEC)
  Process: 9239 ExecStartPre=/bin/echo /.${TESTEXTSERVICESFILES}/test-syslog/bin/nxlog $TESTCONFDATA (code=exited, status=0/SUCCESS)
 Main PID: 9243 (code=exited, status=203/EXEC)

Feb 23 10:11:44 lt-x260-1606.test.local echo[9239]: /./opt/test/extservices/test-syslog/bin/nxlog /storage/test/conf

这次服务无法启动,就像它不想启动以$ {TESTEXTSERVICESFILES}变量开头的进程一样。有人知道即使两种情况下的命令行都一样,为什么它不起作用吗?

于尔根·霍泽尔(JürgenHötzel):

您不能在实际命令中使用变量。systemd.service

要执行的命令必须是绝对路径名。它可能包含空格,但不允许使用控制字符。

您可能不需要将其包装在shell命令中(该命令会进行参数扩展):

ExecStart=/bin/bash -c '/${TESTEXTSERVICESFILES}/syslog/bin/nxlog -c ${TESTCONFDATA}/syslog/nxlog.conf'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Systemd ExecStart中的Bash Brace扩展

系统变量路径在命令提示符中不同

在ExecStart(systemd)中使用sudo

在Angular组件中为服务变量使用Typescript获取器或局部变量

如何在cURL GET命令中为URL使用变量

在Kubernetes中的路径内使用变量

如何使用终端命令从系统路径(`$ PATH`)中删除路径?

如何使用systemd在virtualenv中运行命令

如何在Systemd服务中设置环境变量

systemd服务单元文件中的动态变量

将路径存储在变量中,并在Windows Dos命令行中使用

如何在vbscript中以变量作为路径执行命令

查找命令中的变量设置为bash中的新变量

使用cmd命令中的变量

Systemd ExecStartPre在下一行中的ExecStart之前获取变量

ZSH如何解释命令用作路径的变量中的通配符

使用echo的命令中的变量

在systemd服务文件中从sh脚本设置环境变量

如何在python中为环境路径复制eval命令?

如何使用 Python 中的变量路径导入

存储命令输出为 bash 中的变量

在 systemd 服务中忽略命令的返回状态

如何正确设置我的 systemd 服务 ExecStart?

如何在 .NET systemd 服务中设置环境变量?

如何在特定路径中为 vscode 使用 python 语言服务器?

mkdir 命令中变量的使用

为什么在 systemd 覆盖中的另一个 ExecStart=/new/value 之前使用 ExecStart= (没有值)?

Golang webapp 的 systemd 服务中的 ExecStart 失败

如何在 ExecStart 中执行多个命令