linux - 在 systemd 服务的 ExecStart 命令路径中使用变量

标签 linux service 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 启动测试系统日志; 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 指令并取消注释我得到的第二个指令时 作为失败:

● 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} 变量启动的进程一样。有人知道为什么即使命令行在两种情况下都相同,它也不起作用吗?

最佳答案

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

The command to execute must be an absolute path name. It may contain spaces, but control characters are not allowed.

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

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

关于linux - 在 systemd 服务的 ExecStart 命令路径中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48934097/

相关文章:

Linux 在子目录中递归地从 .bz2 文件中搜索文本字符串

linux - 如何根据用户标识将符号目录链接映射到不同的目录

Android:无法从服务启动管理状态授予 Activity

javascript - 如何在 Linux 中作为守护进程运行的二进制 nodejs 应用程序中运行 exec?

c - 为什么我 fork 的进程将 systemd 作为它们的父进程?

node.js - 将 sudo 与 ExecStart (systemd) 结合使用

linux - 为什么无法在函数中获取shell脚本的参数计数

linux - Docker:安装/共享单个 sock 文件?

linux - 如何从内核中调用 default.target (systemd)

AngularJS ng-repeat Math Pow