我试图让这个变量工作,但我总是得到一个关于systemd-escape的错误。虽然我对特殊字符进行了转义,但变量不起作用:
status="systemctl status syslog-ng | grep Active: | sed 's/.*: //' | sed 's/since.*//g'"我得到的结果是:
Invalid unit name "|" was escaped as "\x7c" (maybe you should use systemd-escape?)
Invalid unit name "|" was escaped as "\x7c" (maybe you should use systemd-escape?)
Invalid unit name "'s/.*:" was escaped as "\x27s-.*:" (maybe you should use systemd-escape?)
Invalid unit name "|" was escaped as "\x7c" (maybe you should use systemd-escape?)
Invalid unit name "'s/since.*//g'" was escaped as "\x27s-since.*--g\x27" (maybe you should use systemd-escape?)
Unit \x7c.service could not be found.
Unit grep.service could not be found.
Unit Active:.service could not be found.
Unit \x7c.service could not be found.
Unit sed.service could not be found.
Unit \x27.mount could not be found.
Unit \x7c.service could not be found.
Unit sed.service could not be found.
● syslog-ng.service - System Logger Daemon
Loaded: loaded (/usr/lib/systemd/system/syslog-ng.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-10-11 14:01:23 CEST; 26min ago
Docs: man:syslog-ng(8)
Main PID: 3020944 (syslog-ng)
Tasks: 3 (limit: 101081)
Memory: 8.9M
CGroup: /system.slice/syslog-ng.service
└─3020944 /usr/sbin/syslog-ng -F -p /var/run/syslogd.pid
Oct 11 14:01:23 syslog-ng systemd[1]: Starting System Logger Daemon...
Oct 11 14:01:23 syslog-ng syslog-ng[3020944]: [2021-10-11T14:01:23.798405] Plugin module not found in 'module-path'; module-path='/usr/lib64/syslog-ng'>
Oct 11 14:01:23 syslog-ng syslog-ng[3020944]: [2021-10-11T14:01:23.801271] Plugin module not found in 'module-path'; module-path='/usr/lib64/syslog-ng'>
Oct 11 14:01:23 syslog-ng syslog-ng[3020944]: [2021-10-11T14:01:23.801828] Plugin module not found in 'module-path'; module-path='/usr/lib64/syslog-ng'>
Oct 11 14:01:23 syslog-ng syslog-ng[3020944]: [2021-10-11T14:01:23.808340] WARNING: With use-dns(no), dns-cache() will be forced to 'no' too!;
Oct 11 14:01:23 syslog-ng systemd[1]: Started System Logger Daemon.我只想将syslog-ng的状态作为变量
发布于 2021-10-11 19:18:21
我们建议关注systemd service unit definition documentation。
如Command lines部分所述。
此语法受shell语法启发,但仅理解以下段落中描述的元字符和扩展,变量的扩展有所不同。具体地说,不支持使用"<“、"<<”、">“和">>”重定向,使用"|“管道,使用"&”在后台运行程序,以及外壳语法的其他元素。
我们不能假设编写systemd服务单元时使用bash或sh语法/规则。
环境变量最好是静态定义的。
以防您需要在动态变量中存储值。我们建议创建一个script service-unit-xyx.bash来执行所有必需的分配,并最终运行目标服务。
使用单个脚本service-unit-xyx.bash,您可以轻松地记录和调试每一行。
最终在ExecStart=命令中执行具有完整路径的service-unit-xyx.bash脚本。
使用执行脚本service-unit-xyx.bash还可以简化服务单元开发,从systemd服务单元文件到bash/sh脚本开发。
还可以使用ExecStartPre=、ExecStartPost=命令来控制服务运行时环境。
阅读并关注互联网上的示例服务单元。
所有命令/脚本都应指定绝对路径引用。
https://stackoverflow.com/questions/69526298
复制相似问题