原文地址python
因爲默認日誌都是在access.log和error.log,日誌文件會愈來愈大,且不方便分析,因此須要對日誌進行切割,一天生成一個日誌文件。nginx
採用python腳本加crontab天天定時切割日誌
python腳本code
#!/usr/bin/env python #encoding=utf-8 import os,datetime yestarday = datetime.datetime.now() - datetime.timedelta(days=1) yestarday = yestarday.strftime("%Y%m%d") print yestarday os.system("mv /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access_%s.log"%(yestarday)) os.system("mv /usr/local/nginx/logs/error.log /usr/local/nginx/logs/error_%s.log"%(yestarday)) os.system("/usr/local/nginx/nginx -s reload")
crontab設置crontab
0 0 * * * root /home/script/nginxLogCut.py