Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Nginx postrotate log script #377

Merged
merged 1 commit into from
Oct 9, 2015
Merged

Fix Nginx postrotate log script #377

merged 1 commit into from
Oct 9, 2015

Conversation

mathew-c
Copy link
Contributor

@mathew-c mathew-c commented Oct 7, 2015

Addresses one of the two issues raised here: #315

Per man service:

The existence of an upstart  job  of  the  same name 
as  a script in /etc/init.d will cause the upstart job
to take precedence over the init.d script.

So, invoke-rc.d nginx rotate >/dev/null 2>&1 fails because the rotate function has been removed since the switch of nginx to Upstart. This is seen when running sudo logrotate -vf /etc/logrotate.d/wordpress-sites on a vanilla bedrock/trellis/sage install, which will result in:

error: error running shared postrotate script for '"/srv/www/**/logs/*.log" '

Of note, the script should send the -USR1 signal to the master process to avoid the old worker processes which are shutting down from still using the old log file(s).

@@ -17,4 +17,4 @@ logrotate_scripts:
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
postrotate: invoke-rc.d nginx rotate >/dev/null 2>&1
postrotate: "[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a really obvious reason why I'm wrong, but can't this just be service nginx reload?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, if you use service nginx reload won't work completely. The new worker processes will log to the correct file(s), but the old worker processes won't. They'll continue to use the old log file(s) as long as they hang around.

It won't be much of a discrepancy, but it is there. The busier the site, the bigger the discrepancy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alan-c is right. This is the recommendend way for Nginx to handle log file reopening. See: http://nginx.org/en/docs/control.html

Alan, you should do $(<"/var/run/nginx.pid"). See : http://stackoverflow.com/questions/21432883/pid-cat-pidfile-or-read-pid-pidfile

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reference @louim. I'll clean up the syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or..... I can ask for input. strace does indicate a performance benefit to read pid < file versus cat file. However, after trying everything I can think of to get the command working inside /etc/logrotate.d/wordpress-sites, I'm stuck with variations along the lines of this...

    PIDfile="/var/run/nginx.pid"
    if test ! -f $PIDfile
    then
      read nginxPID < $PIDfile
      kill -USR1 $nginxPID
    fi

or other similar. All attempts I've tried to get $(<"/var/run/nginx.pid") working with the kill command haven't found success. Not sure if it's an issue with syntax or what, but only works if I split it into two lines (read then kill) and explicitly use read. Depending on what I've tried I get either nothing at all (no error/no successful kill command) or permission denied errors.

Any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@louim Thanks for looking at it. I can get it to work outside ok, but for some reason that is eluding me it's failing inside, including on fresh installs. FWIW, a simple echo as opposed to kill fails inside as well - no output.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just looked a little bit more into it. nginx service actually have a rotate which seems to be exactly what we want. @Alan-c want to try it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll test it out. I had seen a minor reference to it at Stack Exchange when I initially looked at the issue, but I stuck with the kill command based-solution I'd used before.

According to Nginx Essentials [ that's a clean link, not an affiliate one ] on p.40, rotate sends a USR1 signal, so it should be good.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I checked what the rotate command does:

rotate_logs() {
    # Rotate log files
    start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
    return 0
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests work nicely. Good catch, @louim. Command even adds brings a nice message to logrotate -d: * Re-opening nginx log files nginx.

@mathew-c
Copy link
Contributor Author

mathew-c commented Oct 8, 2015

UUOC award presented to the first commit. I humbly accept on it's behalf. 😏 See above convo on the outdated diff.

@austinpray
Copy link
Contributor

This is sick

@swalkinshaw
Copy link
Member

@Alan-c possible to get a squash? Looks good 👍

Using service nginx rotate versus kill pid, per a good catch by @louim.
@mathew-c
Copy link
Contributor Author

mathew-c commented Oct 9, 2015

Squashed it.

@swalkinshaw swalkinshaw changed the title Fix the postrotate script Fix Nginx postrotate log script Oct 9, 2015
swalkinshaw added a commit that referenced this pull request Oct 9, 2015
@swalkinshaw swalkinshaw merged commit 4a927d0 into roots:master Oct 9, 2015
@swalkinshaw
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants