Systemctl bash variable



我试图让这个变量工作,但我总是得到一个关于system -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.*--gx27" (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的状态设置为变量

我们建议遵循systemd服务单元定义文档。

Command lines部分所述。

该语法受到shell语法的启发,但只理解以下段落中描述的元字符和展开,变量的展开不同。具体来说,不支持使用"<<<<"、">>"one_answers">"管道使用" ";在后台运行程序使用"&&quot;以及shell语法的其他元素。

我们不能假设bashsh语法/规则编写systemd业务单元。

环境变量最好静态定义。

如果你需要存储在动态变量值。我们建议创建一个脚本service-unit-xyx.bash来执行所有必需的分配,并最终运行目标服务。

使用单个脚本service-unit-xyx.bash,您可以轻松地记录和调试每一行。

最终在ExecStart=命令中使用全路径执行service-unit-xyx.bash脚本。

使用service-unit-xyx.bash执行脚本也简化了业务单元的开发,从systemd业务单元文件到bash/sh脚本的开发。

还可以使用ExecStartPre=,ExecStartPost=命令来控制服务运行时环境。

阅读并遵循互联网上的示例服务单元。

所有命令/脚本应该指定绝对路径引用。

相关内容

  • 没有找到相关文章