Question

My default logrotate script for apache is:

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

I understand the output and error redirection on line 6 but can someone please explain the purpose of || true? And what are the potential consequences of omitting that bit?

Was it helpful?

Solution

When the logrotate utility runs postrotate (or prerotate) scripts, it checks the error code returned by the script. In particular, when sharedscripts is specified, the error handling is as follows (quoted from man logrotate, emphasis added):

sharedscripts

Normally, prerotate and postrotate scripts are run for each log which is rotated and the absolute path to the log file is passed as first argument to the script. That means a single script may be run multiple times for log file entries which match multiple files (such as the /var/log/news/* example). If sharedscripts is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern, and whole pattern is passed to them. However, if none of the logs in the pattern require rotating, the scripts will not be run at all. If the scripts exit with error, the remaining actions will not be executed for any logs. This option overrides the nosharedscripts option and implies create option.

|| true prevents http reload command from returning an error condition, which avoids the above.

man bash (in the section "Lists") describes ||:

command1 || command2

command2 is executed if and only if command1 returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in the list.

man true explains true in detail, but I think the title suffices: "Do nothing, successfully".

In short, command1 || true first executes command1. If that succeeds, the result is success. Otherwise, it executes true, does nothing successfully, and thus succeeds. So it always executes command1 and always succeeds

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top