Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在systemd服务中为ExecStart使用命令路径中的变量_Linux_Service_Systemd - Fatal编程技术网

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

Linux 在systemd服务中为ExecStart使用命令路径中的变量,linux,service,systemd,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

我正在尝试创建一个系统服务,如下所示:

[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后台程序重新加载”后;sudo systemctl启动测试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指令并取消注释第二个指令时 作为失败:

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

使用
环境文件=
,而不是
来定义多个环境变量

[Service]
Type=simple
EnvironmentFile=/etc/sysconfig/customsyslog
ExecStart=/bin/echo ${TESTEXTSERVICESFILES}/syslog/bin/nxlog $TESTCONFDATA
/etc/sysconfig/customsyslog
将包含
key=value
对,例如

TESTEXTSERVICESFILES=/opt/test/extservices
TESTCONFDATA=/storage/test/conf

不能在实际命令中使用变量:

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

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


我刚刚无意中发现一条旧消息,但看起来您的ExecStart行不起作用,它的开头可能有2'/。一个来自变量前面的ExecStart行,另一个来自声明为/opt/test/extservices的变量
只是想一想。

是的,我知道我之前只是想让这个测试服务工作:)在我的情况下,我必须添加
exec
ExecStart=/bin/bash-c'exec$VAR'
ExecStart=/bin/bash -c '/${TESTEXTSERVICESFILES}/syslog/bin/nxlog -c ${TESTCONFDATA}/syslog/nxlog.conf'