Nginx: host.access.log日志切割

随着几天下来nginx的host.access.log逐渐膨胀.

理想的做法就是根据每天进行区分日志文件,如host.access_20120815.log等, 日志保留最近10天的, 超过10天的日志文件则进行删除.

根据上需求进行nginx的日志切割.

1. 脚本nginxcutlogs.sh

[devwqs@rehserver bin]$ more nginxcutlogs.sh
#!/bin/bash

# 1. move host.access.log to host.access_20120821.log
logs_path="/home/devwqs/nginx/logs/"
mv ${logs_path}host.access.log ${logs_path}host.access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /home/devwqs/nginx/logs/nginx.pid`

# 2. delete all host.access_2012xxxx.log which overtime 10 days
find ${logs_path} -name 'host.*.log' -type f -mtime +10 -exec rm {} \;

  

2. 使用crontab进行把上述脚本发布到定时任务, 每天凌晨0.01分定时执行nginxcutlogs:

#1. 编辑devwqs的crontab定时任务
[devwqs@rehserver bin]$ crontab –e 
1 0 * * * sh /home/devwqs/bin/nginxcutlogs.sh

#2. 重新启动crontab
[devwqs@rehserver bin]$ cd /etc/init.d
[devwqs@rehserver init.d]$ sudo ./crond restart
Stopping crond: [  OK  ]
Starting crond: [  OK  ]

注意: 一定要把crontab重启:

sudo ./crond restart

ubuntu使用: service cron restart

crontab学习资料:

http://blog.csdn.net/sipsir/article/details/3973713

http://os.51cto.com/art/200512/13558.htm

猜你喜欢

转载自greatwqs.iteye.com/blog/1654542