要求:以天为单位进行日志文件的切割,如host.access_20150915.log, 日志保留最近10天的, 超过10天的日志文件则进行删除.
nginxcutlogs.sh脚本内容:
#!/bin/bash
# move host.access.log to host.access_yyyymmdd.log
logs_path="/home/dev/nginx/logs/"
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
mv ${logs_path}host.access.log ${logs_path}host.access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /home/dev/nginx/logs/nginx.pid`
# delete all host.access_20xxxxxx.log which overtime 20 days
find ${logs_path} -name 'host.*.log' -type f -mtime +20 -exec rm {} ;
find ${logs_path} -name 'access_*.log' -type f -mtime +20 -exec rm {} ;
使用crontab进行把上述脚本发布到定时任务, 每天凌晨0点0分1秒定时执行脚本
1 0 * * * /bin/bash /home/dev/nginx/nginxcutlogs.sh
效果: