cat cut-nginx-log.sh
#delete 15 day nginx log 00 00 * * * /bin/bash /data/script/cut-nginx-log.sh [root@ali-idn-pro-nginx-ps01 script]# cat /data/script/cut-nginx-log.sh #!/bin/bash LOGS_PATH=/data/log/nginx/ YESTERDAY=$(date -d "yesterday" +%Y%m%d) #按天切割日志 mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access-${YESTERDAY}.log mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error-${YESTERDAY}.log #向nginx主进程发送USR1信号,重新打开日志文件,否则会继续往mv后的文件写数据的。原因在于:linux系统中,内核是根据文件描述符来找文件的。如果不这样操作导致日志切割失败。 kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'` #delete 15 day nginx log cd ${LOGS_PATH} find /data/log/nginx/ -type f -mtime +15 | xargs rm -f exit 0
定时任务
#delete 15 day nginx log 00 00 * * * /bin/bash /data/script/cut-nginx-log.sh