赞
踩
在containers/下有多个文件夹,每个文件夹都有个同名-json.log的日志文件
一个log文件小到几G大到几时G,导致服务器磁盘被占满程序跑不动,开发要求需要每天一过24:00都给每个日志置空一下,不是直接删除日志文件。
之前我是直接设了个定时任务每天0:0分就把这些日志文件写入为空,调用/dev/null频繁调用太麻烦了。
0 0 * * * echo > /var/lib/docker/containers/11fe311825b93141dcabfad50060a58394886b99bb068b3a9905984b22d3526b/11fe311825b93141dcabfad50060a58394886b99bb068b3a9905984b22d3526b-json.log
0 0 * * * echo > /var/lib/docker/containers/5b14689919647d3fdc036ca88ee1641e876ba83a6a3c7934f27030f9efd89861/5b14689919647d3fdc036ca88ee1641e876ba83a6a3c7934f27030f9efd89861-json.log
0 0 * * * echo > /var/lib/docker/containers/6419145f479b1c56db8810af671a11b8db4fa8fec121c9362438d39752696418/6419145f479b1c56db8810af671a11b8db4fa8fec121c9362438d39752696418-json.log
0 0 * * * echo > /var/lib/docker/containers/6c9abdc970d9a1308b3bb7e95cb2dc5c74e2fa5cc4dcca48e819137d99fbbab0/6c9abdc970d9a1308b3bb7e95cb2dc5c74e2fa5cc4dcca48e819137d99fbbab0-json.log
0 0 * * * echo > /var/lib/docker/containers/9c3a7b6bc295a2c0dbd385eb20765fdfa7ea83a395648bc7c518a6b31da04ebd/9c3a7b6bc295a2c0dbd385eb20765fdfa7ea83a395648bc7c518a6b31da04ebd-json.log
0 0 * * * echo > /var/lib/docker/containers/9d6e49fe926f3bceadd8e214b9741939655c687b0d3b73d93198aaaccf677e6a/9d6e49fe926f3bceadd8e214b9741939655c687b0d3b73d93198aaaccf677e6a-json.log
0 0 * * * echo > /var/lib/docker/containers/a1dbeaf343f11fd3179a618a440edfd8f78d669513d0085919c17980f074ae59/a1dbeaf343f11fd3179a618a440edfd8f78d669513d0085919c17980f074ae59-json.log
0 0 * * * echo > /var/lib/docker/containers/ca3603799481b3e1a799d6918ee56003925ff42cdf72485730c0383ebdb325c4/ca3603799481b3e1a799d6918ee56003925ff42cdf72485730c0383ebdb325c4-json.log
0 0 * * * echo > /var/lib/docker/containers/d6012d2f393003162e81514736a5ea2c5fffc2decad8a96962d9e173ce5978bb/d6012d2f393003162e81514736a5ea2c5fffc2decad8a96962d9e173ce5978bb-json.log
0 0 * * * echo > /var/lib/docker/containers/d8a5c38bb4b68f4609ad60d523c156a8f2843132779f72ca17d41072af63e789/d8a5c38bb4b68f4609ad60d523c156a8f2843132779f72ca17d41072af63e789-json.log
0 0 * * * echo > /var/lib/docker/containers/f281b505abd3fcba801162689353a276575ebf7dc1d0b566cb13da0bdd77b5a8/f281b505abd3fcba801162689353a276575ebf7dc1d0b566cb13da0bdd77b5a8-json.log
0 0 * * * echo > /var/lib/docker/containers/fb7679fece3c4f8c3be219151ea5c8ffebd564fc1fb56e0fa8352dd0e7272b22/fb7679fece3c4f8c3be219151ea5c8ffebd564fc1fb56e0fa8352dd0e7272b22-json.log
0 0 * * * echo > /var/lib/docker/containers/fb895c95d2a621c857e86b7749d7634c3ef90cf9be6d279850057d6fa97af5aa/fb895c95d2a621c857e86b7749d7634c3ef90cf9be6d279850057d6fa97af5aa-json.log
过了个把月后发现磁盘又快占满了,因为一直会有新的容器发布,定时任务里面写死的不能识别到新的,所以现在改良一下,写个脚本。
我脚本文件在/home/clear_log.sh
切换到containers目录下
查询所有的*-json.log日志
然后通过管道符while do循环读出每一行数据并把内容写入空
脚本如下:
#!/bin/bash
cd /var/lib/docker/containers/
ls */*-json.log | while read line
do echo > "$line"
done
OK,功能已经实现了,最后设置定时任务每天0:0分执行一下这个脚本即可。
编辑定时任务:crontab -e
0 0 * * * /home/clear_log.sh
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。