脚本说明:
每天00:00定时切割Nginx日志
[root@check1 ~]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 00 00 * * * root /bin/bash /usr/local/nginx/conf/cut_nginx_log.sh
使用下面脚本,需根据实际路径进行修改
修改下面三项即可:
- OLD_FILE中 +90代表保留90天的日志,90天后的会自动清理
- LOGS_PATH
- PID_PATH
#!/bin/bash #CUT_NGINX_LOG #Version:1.0 LOGS_PATH="/usr/local/nginx/logs" YESTERDAY=`date -d "yesterday" +"%Y-%m-%d"` PID_PATH="/usr/local/nginx/logs/nginx.pid" OLD_FILE=`find $LOGS_PATH -mtime +90 -type f -name "ERROR*"` if [ ! -d "$LOGS_PATH/old_access" ] then mkdir $LOGS_PATH/old_access fi if [ ! -d "$LOGS_PATH/old_error" ] then mkdir $LOGS_PATH/old_error fi mv ${LOGS_PATH}/access.log ${LOGS_PATH}/old_access/ACCESS_${YESTERDAY}.log mv ${LOGS_PATH}/error.log ${LOGS_PATH}/old_error/ERROR_${YESTERDAY}.log kill -USR1 `cat ${PID_PATH}` if [[ ! -z $OLD_FILE ]] then for a in $OLD_FILE do TIME=`date` echo "delet $a $TIME" >> /var/log/messages rm -rf $a done fi