Clean /var/log/nginx logs file

10,863

To control application's lifecycle Unix provides a mechanism called Unix signals. USR1 is custom and usually handles the log rotation, other signal like HUP is standard and performs reload.

http://nginx.org/en/docs/control.html

TERM, INT   fast shutdown
QUIT    graceful shutdown
HUP changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new configuration, graceful shutdown of old worker processes
USR1    re-opening log files
USR2    upgrading an executable file
WINCH   graceful shutdown of worker processes

Before send a signal to PID rename the file. After you rename it log entries will still be going into the same file because inode hasn't been changed.

cd /var/log/nginx
mv access.log access.log.old
mv error.log error.log.old
kill -USR1 `cat /var/run/nginx.pid`
Share:
10,863
angelokh
Author by

angelokh

Founder, Swifteam, Inc.

Updated on June 14, 2022

Comments

  • angelokh
    angelokh almost 2 years

    I have over 10.0G logs under /var/log and /var/log/nginx. How can I safely clean it?

    7.8G /var/log/nginx/custom 2.0G /var/log/nginx 2.0G /var/log

  • Anatoly
    Anatoly over 8 years
    @angelokh Nginx error and access log should both rotate. Maybe you ask about not Nginx logs? It depends on the application, usually USR1 supported by vast majority Linux applications