切割日志

# 全服a/nginx日志切割全集
如下是运用logrotate工具切割**a端日志**

```
vim /etc/logrotate.conf/channelHandle
/game/publish/pm2/*.log
{
su root root
daily
missingok
copytruncate
notifempty
dateext
sharedscripts
postrotate
/opt/scripts/move_old_log.sh
endscript
}
```
```

mkdir -p /opt/scripts
vim /opt/scripts/move_old_log.sh
```
```

#!/bin/bash
datestamp=$(date +%Y%m%d)
logdir=/game/log/$datestamp
mkdir -p $logdir
mv /game/publish/pm2/*log-$datestamp $logdir
find /game/log -mindepth 1 -maxdepth 1 -mtime +60 \
-type d | xargs rm -rf
```
```

chmod +x /opt/scripts/move_old_log.sh

如下更改3-22点的时间即可
[root@logstash ash]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

```

如下是切割**nginx日志要点**
重点是postrotate
```
[root@logstash ash]# cat /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log
{
su root root
daily
missingok
copytruncate
notifempty
dateext
sharedscripts
postrotate
/opt/scripts/move_old_log.sh
/bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}
```
如下脚本内容不变,都是生成当前时间,输出到新的目录作为旧日志文件存放位置
```
[root@logstash ash]# cat /opt/scripts/move_old_log.sh
#!/bin/bash
datestamp=$(date +%Y%m%d)
logdir=/tmp/$datestamp
mkdir -p $logdir
mv /usr/local/nginx/logs/*.log-$datestamp $logdir
find /tmp -mindepth 1 -maxdepth 1 -mtime +60 \
-type d | xargs rm -rf
```

猜你喜欢

转载自www.cnblogs.com/capable/p/12171213.html
今日推荐