Tomcat 使用 jsvc 以守护进程的方式启动(daemon.sh )。这样tomcat自身将会生成另外一个日志文件(catalina-daemon.out),而不是之前的catalina.out,而且catalina-daemon.out日志不会自动切割,会越来越大。

以前遇到过一个问题,就是网站突然访问空白,排查到最后发现是当前进行了网站打包备份操作,有一个超过2GB的压缩包。删掉后立马页面访问正常。具体原因还不清楚。

同时从运维角度和日志分析角度思考,日志文件最好做切割处理,并日志文件不宜过大。

想了想,还是使用linux的crontab的定时任务吧,

编写一个shell脚本,脚本放到 /etc/cron.daily目录下,代码如下:

#!/bin/bash
thedate=`date --rfc-=date`
predate=`date +%Y-%m-%d --date="-7 day"` rmfile="/xxxx/server/tomcat/logs/catalina-daemon.${predate}.out"
outfile="/xxxx/server/tomcat/logs/catalina-daemon.out"
if [ -f ${rmfile} ];then
rm -f ${rmfile}
fi if [ -f ${outfile} ];then
cp ${outfile} /xxxx/server/tomcat/logs/catalina-daemon.${thedate}.out
echo "" > ${outfile}
fi exit
#!/bin/bash
thedate=`date --rfc-=date`
predate=`date +%Y-%m-%d --date="-7 day"` rmfile1="/xxxxx/server/tomcat/logs/catalina-daemon.${predate}.out"
rmfile2="/xxxxx/server/tomcat/logs/p2p.log.${predate}"
rmfile3="/xxxxx/server/tomcat/logs/localhost.${predate}.log"
rmfile4="/xxxxx/server/tomcat/logs/host-manager.${predate}.log"
rmfile5="/xxxxx/server/tomcat/logs/catalina.${predate}.log"
outfile="/xxxxx/server/tomcat/logs/catalina-daemon.out" for rmfile in "${rmfile1}" "${rmfile2}" "${rmfile3}" "${rmfile4}" "${rmfile5}"
do
if [ -f ${rmfile} ];then
rm -f ${rmfile}
fi
done if [ -f ${outfile} ];then
cp ${outfile} /xxxxx/server/tomcat/logs/catalina-daemon.${thedate}.out
echo "" > ${outfile}
fi exit

检查是否配置好了自动执行配置 ,cat /etc/crontab,没有就加行下文的红色部分。

[root@xxxxServer /]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/ # For details see man crontabs # Example of job definition:
# .---------------- minute ( - )
# | .------------- hour ( - )
# | | .---------- day of month ( - )
# | | | .------- month ( - ) OR jan,feb,mar,apr ...
# | | | | .---- day of week ( - ) (Sunday= or ) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed #run-parts
* * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily //每天执行/etc/cron.daily内的脚本 
* * root run-parts /etc/cron.weekly
* * root run-parts /etc/cron.monthly

查看crond服务是否运行:

pgrep crond

/sbin/service crond status

ps -elf|grep crond|grep -v "grep"

crond服务操作命令:

/sbin/service crond start //启动服务  
/sbin/service crond stop //关闭服务  
/sbin/service crond restart //重启服务  
/sbin/service crond reload //重新载入配置

设置crond随机启动

chkconfig crond on 

好了,现在就不用担忧日志文件过大问题了!

同时上面的脚本会每天运行一次,并删除七天之前的日志文件,具体时间,可自己设定。

PS:

http://www.cnblogs.com/xd502djj/archive/2010/12/29/1919478.html

http://www.jb51.net/article/34332.htm

http://www.cnblogs.com/panblack/archive/2013/05/30/split_tomcat_catalina_out.html

http://desert3.iteye.com/blog/1393541

Tomcat catalina-deamon.out 日志切割 每天生成一个文件的更多相关文章

  1. 将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件

      将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件import java.io.File;import java.io.FileOutputStre ...

  2. spark中saveAsTextFile如何最终生成一个文件

    原文地址: http://www.cnblogs.com/029zz010buct/p/4685173.html 一般而言,saveAsTextFile会按照执行task的多少生成多少个文件,比如pa ...

  3. Shell 命令行 从日志文件中根据将符合内容的日志输出到另一个文件

    Shell 命令行 从日志文件中根据将符合内容的日志输出到另一个文件 前面我写了一篇博文Shell 从日志文件中选择时间段内的日志输出到另一个文件,利用循环实现了我想要实现的内容. 但是用这个脚本的同 ...

  4. Shell 从日志文件中选择时间段内的日志输出到另一个文件

    Shell 从日志文件中选择时间段内的日志输出到另一个文件 情况是这样的,某系统的日志全部写在一个日志文件内,所以这个文件非常大,非常长,每次查阅的时候非常的不方便.所以,相关人员希望能够查询某个时间 ...

  5. 100个Shell脚本——【脚本8】每日生成一个文件

    [脚本8]每日生成一个文件 要求:请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为)2017-07-05.log, 并且把磁盘的使用情况写到到这个文件中,(不用考虑c ...

  6. tomcat 使用log4j进行日志切割

    因为tomcat catalina.out日志不会自己主动切割, 一.日志切割所需包在附近中 1. 压缩包中有三个jar包:     log4j-1.2.16.jar      tomcat-juli ...

  7. 配置好Nginx后,通过flume收集日志到hdfs(记得生成本地log时,不要生成一个文件,)

    生成本地log最好生成多个文件放在一个文件夹里,特别多的时候一个小时一个文件 配置好Nginx后,通过flume收集日志到hdfs 可参考flume的文件 用flume的案例二 执行的注意点 avro ...

  8. 使用IO流将数据库中数据生成一个文件,结果使用Notepad++打开部分数据结尾出现NUL

    场景描述: 项目中通过java代码中从数据库中查询一系列数据,对数据做相应处理,然后通过字符流将数据写如一个新生成的文件中,将该项目部署在linux服务器上,最后生成的文件拿到本地使用notepad+ ...

  9. 分割catalina.out 每天生成一个文件

    1. touch xxx(文件名字).sh 2.     vim xxx.sh 写入  ----------------------- #!/bin/sh cd `dirname $0`pwdd=`d ...

随机推荐

  1. Android开发,在Fragment中,隐藏或关闭软键盘(虚拟键盘)的方法

    网上可以搜到各种方法,实际测试中,我的开发用机中,仅仅下面这个方法有效,记录一下. //隐藏虚拟键盘      public static void HideKeyboard(View v)      ...

  2. mysql查找有某列但没有此列索引的表

    select a.TABLE_SCHEMA,a.TABLE_NAME from information_schema.`COLUMNS` a left join (select 'etl_stamp' ...

  3. border-radius背景色超出圆角问题解决

    span{ display: block; background: #f4f4f4; color: #333; font-size: 14px; -webkit-border-radius: 20px ...

  4. PCL关键点(1)

    关键点也称为兴趣点,它是2D图像或是3D点云或者曲面模型上,可以通过定义检测标准来获取的具有稳定性,区别性的点集,从技术上来说,关键点的数量相比于原始点云或图像的数据量减小很多,与局部特征描述子结合在 ...

  5. gitlab安装与配置(Centos6.8)

    0.Centos7请参照官方文档 https://about.gitlab.com/installation/#centos-7 1. Install and configure the necess ...

  6. 理解javascript中的回调函数(callback)【转】

    在JavaScrip中,function是内置的类对象,也就是说它是一种类型的对象,可以和其它String.Array.Number.Object类的对象一样用于内置对象的管理.因为function实 ...

  7. android studio 解析Excel数据格式导入poi-3.17.jar时的一系列报错及处理Failed resolution of: Ljavax/xml/stream/XMLEventFactory,duplicate entry: org/apache/xmlbeans/xml/stream/Location.class,GC overhead limit exceeded

    在org官网下载的poi jar包,导入到studio compile files('libs/poi-3.17.jar') compile files('libs/poi-ooxml-3.17.ja ...

  8. 【协议学习】SIP基本场景分析

    1.SIP业务基本知识 1.1 业务介绍 会话初始协议(Session Initiation Protocol)是一种信令协议,用于初始.管理和终止网络中的语音和视频会话,具体地说就是用来生成.修改和 ...

  9. 【Qt开发】QThread 实用技巧、误区----但文档中没有提到

    本文主要内容: 在任务一中,用 四 种方式实现:点击界面按钮,开线程运行一段程序,结果显示在一个Label上.1. 用不正确的方式得到看似正确的结果2. 用Qt Manual 和 例子中使用的方法3. ...

  10. Linux修复系统命令

    fsck.ext2(file system check-second filesystem ) 功能说明:检查文件系统并尝试修复错误. 语法:fsck.ext2 [-acdfFnprsStvVy][- ...