logrotate "postrotate" configuration

Below are 2 identical examples of logrotate configuration, the only difference is the postroate block where the second example is missing the >/dev/null 2>&1 || true

/var/log/nftables.log {
  size +10M
  maxage 30
  sharedscripts
  postrotate
    /usr/bin/systemctl kill -s HUP rsyslog.service >/dev/null 2>&1 || true
  endscript
}

/var/log/nftables.log {
  size +10M
  maxage 30
  sharedscripts
  postrotate
    /usr/bin/systemctl kill -s HUP rsyslog.service
  endscript
}

I would like to know why is 2nd example bad (I assume it is) and please explain what exactly
>/dev/null 2>&1 || true does?

I mean the meaning of each of the characters: >/dev/null and 2>&1 and || and true

Asked By: metablaster

||

This is basic bash syntax. You should read and reread man bash or any Linux Shell tutorial or book.

But, here goes:

>/dev/null redirects the STDOUT stream to the NULL (infinite data sink) device, throwing it away.

2>&1 redirects the STDERR stream to STDOUT (which has been redirected to /dev/null).

|| is the Logical OR operator. If (and only if) the expression to the LEFT of || fails (returns non-zero $STATUS), the expression to the RIGHT of || is evaluated and returned as the result of the whole expression.

true is a program that does nothing except return a SUCCESS $STATUS.

In summation, /usr/bin/systemctl kill -s HUP rsyslog.service >/dev/null 2>&1 || true means:

Execute the command, discarding output and error messages. If the command succeeds, return SUCCESS. If
the command fails, return SUCCESS.

Beware: the postrotate commands are interpreted by /bin/sh, not /bin/bash, so any bash extended features will not work.

Answered By: waltinator
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.