强烈推荐使用 logrotate 来完成这个需求,具体中使用了 logrotate 中的 copytruncate 方法:
社区[1]中给出了一个使用的示例,如下:
/home/bea1/basedomain/bin/nohup.out{ daily copytruncate dateext olddir /home/bea1/basedomain/bin nocompre rotate 100 }
另外[2]中的评论中给出了具体机制的示例,如下:
Send SIGSTOP to the process, copy the log to another name, truncate the log, send SIGCONT to the process, perhaps like this:
pkill -STOP legacyappname cp /var/log/something.log /var/log/something.log.backup cat /dev/null > /var/log/something.log pkill -CONT legacyappname
You could also have logrotate do the magic for you with carefully crafted pre and post rotation scripts and the copytruncate option, like so:
/var/log/something { daily rotate 5 copytruncate prerotate # This assumes you have a pid file, of course. # If you don't, this could instead be a pkill like above. kill -STOP `cat /var/run/legacyappname.pid` endscript postrotate kill -CONT `cat /var/run/legacyappname.pid` endscript }
参考阅读:
1. 利用Logrotate实现weblogic日志每日备份
2. https://serverfault.com/questions/189477/rotate-logs-of-a-dumb-non-interactive-application/189880#189880
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于