a, script ideas
the first step is to rename the log file, don't worry about it after the rename nginx lose log can't find the log file. In you before to open the original name of the log file, nginx will rename the file to you write the log, Linux is to rely on the file descriptor instead of locating files in the file name.
the second step is to send USR1 signal nginx main process.
nginx main process after receiving signal will be read from a configuration file into the log file name, to open the log file (named after log in the configuration file name), and the working process of the user as the owner of the log file.
to open the log file, nginx main process will close the namesake, the log file and inform the worker process using the new open log file.
work process immediately open a new log file and close the rename of the log file.
then you can deal with the old log files.
2, script for
nginx log by date automatically cutting script as follows:

code is as follows:



#nginx log cutting script

#!/bin/bash
#set the log file directory contains
logs_path="/usr/local/nginx/logs/" #set the pid file

pid_path="/usr/local/nginx/nginx pid "


mv #rename log files. ${logs_path} access log ${logs_path} access_ $(date - d "yesterday" + "% Y % m % d"). The log

#to nginx main process signals to open the log
kill USR1 ` cat ${pid_path} `

save the above script nginx_log. Sh, and set the timer cutting task

Three work, timing,

Settings in crontab job

code is as follows:


0 0 * * * bash/usr/local/nginx/nginx_log sh
this is a day of zero 0 the nginx log renamed date format, and regenerate new log files today.

you might be interested in the article:

  • batch conversion directory file shell script code
  • a download page picture shell script
  • a test the download speed of shell script


This concludes the body part