0

I'm trying to get this variable work, but I'm getting always an error about systemd-escape. Although I escape the special character, the variable is not working:

status="systemctl status syslog-ng | grep Active: | sed 's/.*: //' | sed 's/since.*//g'"

The result I'm getting is here:

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.

I just want to have the status of my syslog-ng as a variable

caesim
  • 37
  • 1
  • 2
  • 9
  • 1
    `status=$(systemctl is-active syslog-ng )` – Jetchisel Oct 11 '21 at 12:42
  • as a single command it would work, but as a variable I'm getting this: `-bash: active: command not found` – caesim Oct 11 '21 at 12:45
  • 3
    Why are you assigning a string, instead of executing a command? Read https://stackoverflow.com/questions/5615717/how-to-store-a-command-in-a-variable-in-a-shell-script and https://mywiki.wooledge.org/BashFAQ/050 . `as a single command it would work, but as a variable` What do you mean by "single command" and what do you do with the variable? You seem to be trying to execute the content of the varialble - don't, just print it. `status=$(systemctl is-active syslog-ng ) ; echo "$status"`. Check your scripts with shellcheck . – KamilCuk Oct 11 '21 at 12:46
  • this `systemctl is-active syslog-ng`work but not this `status=$(systemctl is-active syslog-ng)` – caesim Oct 11 '21 at 12:49
  • 1
    status=$(systemctl is-active syslog-ng ) ; echo "$status" looks good to me – caesim Oct 11 '21 at 12:50
  • As an aside, the [`grep` is useless](https://www.iki.fi/era/unix/award.html#grep) and you should merge the two `sed` scripts; `systemctl status syslog-ng | sed -n '/Active: /!d;s///;s/since.*//p` (the `/g` flag could not possibly be useful here, either). – tripleee Oct 11 '21 at 19:36

1 Answers1

0

We suggest to follow systemd service unit definition documentation.

As stated in the Command lines section.

This syntax is inspired by shell syntax, but only the meta-characters and expansions described in the following paragraphs are understood, and the expansion of variables is different. Specifically, redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported.

We cannot assume bash nor sh syntax/rules writing systemd service unit.

Environment variables are better defined statically.

In case you need to store values in dynamic variables. We suggest create a script service-unit-xyx.bash that executes all the required assignments and eventually run the target service.

Using a single script service-unit-xyx.bash you can easily log and debug every line.

Eventually execute service-unit-xyx.bash script with full path in ExecStart= command.

Using execution script service-unit-xyx.bash also simplify the service unit development, from systemd service unit file to bash/sh script development.

It is also possible to use a ExecStartPre=, ExecStartPost= commands to control service runtime environments.

Read and follow sample service units from the internet.

All commands/scripts should specify absolute path reference.

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30