Can use tail
to rotate a nginx log file in orderer to have a single log file capped in size?
tail -c 1000000 access.log > temp.log
mv temp.log access.log
kill -USR1 `cat /var/run/nginx.pid`
The above would be executed in a cron job (running every 5 minutes) every time access.log
exceeds 1100000
bytes.
The last line tells nginx to reopen log files (as the second line deletes the original access.log
)
I've read about variuos methods to rotate (nginx) logs but I've never read about this approach.
This way I end up having a single file capped in size.
And rotation is not applied to a set of files but to the log entries inside a single log file.
Of course tail -c
(as from comments) will truncate the first (oldest line).
The major issue with that is that while tail is writing the truncate file the process is still writing log entries so some entries may get lost.
Bottom note:
I'm aware of logrotate
and newsyslog
tools, I've read the main parts of the documentation and they are also trivial to setup for ordinary scenarios.
My attempt is not to reinvent the wheel. I'm just "curious" to know if things can be done in a different way.
The answer probably is no for very good reasons.
I'm just trying to have a better understanding on the subject.