Nginx real case - how to cut the log
1. What is the log cutting
- Cutting logs is to log system for storage in days, per day logs are open-minded
- Enterprises which have backed up yesterday's daily log, and then generate a new log file records Today's log
- If you do not do the log cutting, when a large log file when, vim stuck open when possible, etc.
- And the time to troubleshoot the log after cutting too simple, is the day of issue on the investigation day log
2. Why was cut logs
In a production environment every day to pack log files are backed up
if both manually every day to intercept the log, rename this is very inconvenient
so we write a script and create a scheduled task to make these work
logs cut is performed automatically , writing in crotab regular tasks, and write a script to achieve
3. Manually achieve logs cut
nginx server log types:
Log Name | Log Type |
---|---|
access.log | Stored successfully access nginx server log information |
error.log | Nginx server storage access failure log information |
nginx.pid | Strictly speaking this is not a log, which is stored pid nginx process |
Server:
cd /usr/local/nginx/
cd logs/
ls #访问日志都在里面
In the client (real machine):
ab -c 1 -n 10000 http://www.westos.org/index.html #访问服务端10000次
Server:
du -sh access.log #查看文件大小
mv access.log `date +%F -d -1day`_access.log #管理日志
nginx -s reopen #重新获得新的access.log文件
4. The script logs cut way to achieve
Write a script on the server:
cd /usr/local/nginx/logs
vim backup.sh
1 #!/bin/bash
2 LOGS_PATH=/usr/local/nginx/logs/oldlogs
3 CUR_LOGS_PATH=/usr/local/nginx/logs
4 YESTERDAY=$(date +%F -d -1day)
5
6 mv $CUR_LOGS_PATH/access.log $LOGS_PATH/${YESTERDAY}_access.log
7 mv $CUR_LOGS_PATH/error.log $LOGS_PATH/${YESTERDAY}_error.log
8
9 /usr/local/nginx/sbin/nginx -s reopen
Set permissions to create the directory:
chmod +x backup.sh
mkdir oldlogs
ll
Execute scripts, view the log whether to automatically cut backup:
./backup.sh
cd oldlogs/
ll
Write regular tasks: the server to 00:00 every day on time for log cutting
crontab -e
写入:
0 0 * * * /bin/bash /usr/local/nginx/logs/backup.sh
crontab -l #显示定时任务