简体   繁体   English

Catalina.out在由bash脚本编辑后未记录

[英]Catalina.out not logging after edition by bash script

This is a tomcat7 installation with the default logging configuration, the catalina.out is only being rolled out when we restart the server. 这是具有默认日志记录配置的tomcat7安装,仅当我们重新启动服务器时才推出catalina.out。 As it is prod server we can not restart it very often. 由于它是产品服务器,因此我们不能经常重新启动它。 We have a huge number of entries going to that file which causes our catalina.out to grow very high in few days until it consumes the whole diskspace. 我们有大量进入该文件的条目,这导致我们的catalina.out在几天之内变得非常高,直到耗尽整个磁盘空间为止。

As we don't want to change the logging configuration as it is puppetized and we would need to create devops tickets and all that slow stuff, I wrote a bash script that is being run every 5 min via crontab that will cut the log file by half when a limit is reached, the script is like the following: 由于我们不想更改已配置的日志记录配置,而我们需要创建devops票证和所有较慢的东西,因此我编写了一个bash脚本,该脚本每隔5分钟通过crontab运行一次,该脚本将通过达到限制的一半时,脚本如下所示:

 if [ $catalinaSize -gt $catalinaThreshold ]; then
  middle=$(wc -l $catalinaLoc | awk '{ print $1 }')
  middle=$(( $middle / 2 ))
  sed -i -e 1,${middle}d $catalinaLoc
  echo "+++ catalina.out was cut by half"

Basically this script checks the current size of the file and compares it to a threshold value, then it uses wc and awk to retrieve the number of lines in that file so it can use then sed for cutting the file by half. 基本上,该脚本检查文件的当前大小并将其与阈值进行比较,然后使用wc和awk检索该文件中的行数,以便可以使用sed然后将其剪切一半。

I tested the script in other environments and it worked. 我在其他环境中对该脚本进行了测试,并且可以正常工作。 The problem is that after some successful runs for several days in production, suddenly the catalina.out is not getting any log entries from tomcat since some days ago. 问题是在生产中成功运行了几天后,突然之间,catalina.out从几天前就没有从tomcat获得任何日志条目。

The explanation I think about is that Tomcat is not able to write into that file anymore because of the cut by half operation. 我考虑的解释是,由于减少了一半的操作,Tomcat不再能够写入该文件。

Is it possible to know what is preventing Tomcat to write into that file? 是否有可能知道是什么导致Tomcat无法写入该文件?

I suspect it is sed -i doing the damage: behind the scenes, it writes the output stream to a temp file, then moves the temp file to the original name. 我怀疑这是sed -i造成的损害:在后台,它将输出流写入临时文件,然后将临时文件移动到原始名称。 I suspect the file handle held by catalina no longer points to any file. 我怀疑由catalina持有的文件句柄不再指向任何文件。

You'll have to find a way to actually edit the file, not replace it. 您将必须找到一种方法来实际编辑文件,而不是替换文件。 This might be a valid replacement for sed : 这可能是sed的有效替代品:

printf "%s\n" "1,${middle}d" "wq" | ed "$catalinaLoc"

Tangentially, an easier way to get the number of lines: 切线地,更简单的方法来获得行数:

middle=$(( $(wc -l < "$catalinaLoc") / 2 ))

When you redirect to wc , it no longer prints out the filename. 当您重定向到wc ,它将不再打印出文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM