-1

I have some problems with nginx log rotation - nginx -s reopen does not reopen the log file, this is why I am researching the topic.

In several places, I found this script:

$ mv access.log access.log.0
$ kill -USR1 `cat master.nginx.pid`
$ sleep 1
$ gzip access.log.0    # do something with access.log.0

My question is why there is sleep after kill? It seems unnecessary, because even reopen slows, gzip will not be able to compress everything for 1 second anyway?

Do they do it this way in case file is very small, so no data to be lost?

Nick
  • 826
  • 2
  • 15
  • 42

1 Answers1

2

The sleep is there to allow the program to cleanly close the log file before the gzip compresses it.

Remember, once a file is opened, the program writes to it through a file descriptor, the name is now irrelevant. Renaming the file only affects the entry in the directory. When you send a signal to the program to reopen it's log file, It will first flush the buffers then close the file and finally reopen it. The 1 second delay allows for that to happen.

user9517
  • 115,471
  • 20
  • 215
  • 297
  • thanks. this was what I thought too. next question is to find why my nginx not reopen the files at all - it creates file with length zero, but never writes inside it. – Nick Aug 26 '16 at 07:41
  • @Nick did u check the file permission of the newly created file? – eranga Aug 26 '16 at 07:46
  • yes, all is OK. currently looks like nginx problem, which can not be because I have 20+ servers with same conf and same binary nginx version and happen only on one of the servers. however I still have no enough information to post it online. However my script currently missing this `sleep`. – Nick Aug 26 '16 at 08:00
  • I found the bug. Seems nginx rotate the logs as www user - e.g. nobody / wwwdata etc. In my case, this user have no rights to write in logs directory. It is still strange, because it actually creates the log file, but it can not write inside. I will investigate this future, but will be done in several weeks. pls drop me a comment if you are interested to know more. – Nick Aug 30 '16 at 10:19