`
brandNewUser
  • 浏览: 446058 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

解决jenkins产生的日志过大以及一些衍生问题

阅读更多
 
 
 
jenkins使用一段时间后,会导致出现比较大的日志问题,经常占满硬盘空间(因为我们使用的硬盘大小20G,无额外存储要求)。在硬盘空间占满之后,会导致一些基本的命令都无法使用,譬如tab都不能出结果。
 
其中显示的日志,就例如下面的样例:
 
 
        question:      [DNSQuestion@1138295053 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@815573059 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@41696207 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@2028905592 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@1941181185 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@641688452 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@1165924047 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@1220425596 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@465635697 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@1186949838 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@2009482296 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@1316653163 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@1575193172 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@1622635068 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
        question:      [DNSQuestion@630525334 type: TYPE_IGNORE index 0, class: CLASS_UNKNOWN index 0, name: ]
 
而且我们将已经定位到的文件删除掉,仍然不能释放空间,经过查看可以深层次发现其中的问题。
 
未释放磁盘空间原因
 
在Linux或者Unix系统中,通过rm或者文件管理器删除文件将会从文件系统的文件夹结构上解除链接(unlink).然而假设文件是被打开的(有一个进程正在使用),那么进程将仍然能够读取该文件,磁盘空间也一直被占用。而我删除的是jenkins的日志文件,如果jenkins服务没有停止,此时删除该文件并不会起到什么作用。
 
删除的时候文件应该正在被使用
 
当linux打开一个文件的时候,Linux内核会为每个进程在/proc/ 『/proc/nnnn/fd/文件夹(nnnn为pid)』建立一个以其pid为名的文件夹用来保存进程的相关信息,而其子文件夹fd保存的是该进程打开的全部文件的fd(fd:file descriptor)。
 
kill进程是通过截断proc文件系统中的文件能够强制要求系统回收分配给正在使用的的文件,这是一项高级技术,仅到管理员确定不会对执行中的进程造成影响时使用。应用程序对这样的方式支持的并不好,当一个正在使用的文件被截断可能会引发不可预知的问题,所以最终还是采用停止jenkins应用来解决该问题。
 

    当一个文件正在被一个进程使用时,用户删除此文件,文件只会从目录结构中删除,但并没有从磁盘删除。当使用这个文件的进程结束后,文件才会真正的从磁盘删除,释放占有的空间。

 
    我们发现剩余磁盘空间比较少时,回去删除一些大的临时文件或者log文件,如果删除之后会发现磁盘空间并未减少,那么可以通过“lsof”命令去查看正在使用该文件的进程,然后再重启该进程或者服务。
 
一般情况下,jenkins的部署常用几种方式:
 
  1. 通过系统服务安装并启动:service jenkins start/stop/restart,此时就可以通过命令来停止;
  2. 将war包部署至tomcat中,此时stop tomcat服务器就可以了。
 
而jenkins的日志问题经过google一番,找出相应的几个解决方法:
 
 
 
先考虑在jenkins上安装两个插件:
 
 

You can use the Logfilesizechecker Plugin:

This plugin monitors the size of the output file of a build and aborts the build if the log file gets too big.

Or, if this has also an impact on the runtime, the Build-timeout Plugin:

This plugin allows you to automatically abort a build if it's taking too long. Once the timeout is reached, Jenkins behaves as if an invisible hand has clicked the "abort build" button.

 
 
在jenkins中也已经意识到了该问题,并有了初步的解决方案:
 
 
根据朱迪的调研,考试使用下面的方式来解决此问题:
 
 
 
This seems to be due to DNS multicast as explained here: https://issues.jenkins-ci.org/browse/JENKINS-25369
Workaround: add -Dhudson.DNSMultiCast.disabled=true to JAVA_ARGS.
PS: I'm answering my own question here on Stack Overflow because I couldn't find the answer on Google easily, and it will be useful to other people running Jenkins.
  
 
日志中出现过多的DNS相关错误。
 
此外,还有一些其他需要值得注意的点,例如在脚本中如果涉及到启动进程的话,需要加入BUILD_ID,否则该进行启动后就会被kill掉。
 
如果不设置BUILD_ID,则jenkins在结束自己的脚本执行时会将创建的所有subprocess kill掉,BUILD_ID是Jenkins的一个环境变量,如果不随便改成一个值,那么由于startup.sh是fork一个进程执行的,Jenkins执行完所有脚本就会退出,带着subprocess一起死掉,具体的解释原因详见:
 
 
分享到:
评论
1 楼 ccor 2017-04-18  
linux下清空正在使用的文件命令:
:> xxx.log

相关推荐

Global site tag (gtag.js) - Google Analytics