> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/centos/shell/nginx-ri-zhi-qie-ge-jiao-ben-an-tian-qie-ge.md).

# Nginx日志切割脚本（按天切割）

**Nginx日志切割脚本（按天切割）**

> **让日志按天存放** **每天凌晨00:00把前一天的Nginx日志access.log重命名为access-xxxx-xx-xx.log格式** **例如：access-2018-5-20.log**

**\[root\@localhost \~]# cat /data/scripts/auto\_cuts\_nginx\_log.sh**

```bash
#!/usr/bin/env bash
# 2018-5-20 21:24:20
# Automatic Cutting of Nginx logs
nginx=(
    [0]=access.log
    [1]=/usr/local/nginx
    [2]=access_`date +%Y%m%d`.log
    [3]=error.log
    [4]=error_`date +%Y%m%d`.log
    [5]=`ps -ef |grep nginx|awk '/master/ {print $2}'`
)
#分割日志
mv ${nginx[1]}/logs/${nginx[0]} ${nginx[1]}/logs/${nginx[2]}
mv ${nginx[1]}/logs/${nginx[3]} ${nginx[1]}/logs/${nginx[4]}
#kill -USR1 PID向Nginx主进程发送USR1信号。USR1信号是重新打开日志文件
kill -USR1 ${nginx[5]}
if [ $? -eq 0 ]
then
echo -e "\033[32m The ${nginx[2]} Cutting Successfully \033[0m"
echo -e "\033[32m The ${nginx[4]} Cutting Successfully \033[0m"
fi
```
