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. 《VS2010/MFC编程入门教程》——读书笔记

    推荐两个比较好的学习网站:http://v.dxsbb.com/jisuanji/555/ http://www.jizhuomi.com/software/257.html MFC全称Microso ...

  2. JS不同浏览器图片载入处理

    //不同浏览器图片加载判断 p.loadImgVerify = function(oimg,fn){ //载入发起请求 加入JS单线程队列事件(当状态满足时候执行些事件) if (qp_shared. ...

  3. OpenGL透明与混色效果

    一.理论讲解 在OpenGL中,物体透明技术通常被叫做混合(Blending). 透明是物体(或物体的一部分)非纯色而是混合色,这种颜色来自于不同浓度的自身颜色和它后面的物体颜色. 一个有色玻璃窗就是 ...

  4. Mac eclipse 编译、调试c++ 程序

    可以先安装个CDT插件: eclipse菜单 -> Help -> Install New Software... -> Work with (Add..) Name:CDT Loc ...

  5. C++多线程中调用python api函数

    错误场景:一直等待全局锁. 解决方法: 一.首先定义一个封装类,主要是保证PyGILState_Ensure, PyGILState_Release配对使用,而且这个类是可以嵌套使用的. #inclu ...

  6. Eclipse/MyEclipse上配置Spring环境

    在MyEclipse上配置Spring环境 myeclipse其实已经集成Spring的开发环境,我们只需在新建的项目上添加spring的配置环境就可以 新建一个java项目 选中创建好的项目之后,在 ...

  7. R绘图系统中的坐标系

    在R语言中,对于图中的点来说,有很多种坐标系来进行定位 举个例子: par(omi = c(1, 1, 1, 1), mai = c(1, 1, 1, 1), mfrow = c(1, 2)) plo ...

  8. C# Bitmap转化为BitmapImage方法

    public BitmapImage BitmapToBitmapImage(Bitmap bitmap) { Bitmap bitmapSource = new Bitmap(bitmap.Widt ...

  9. 分享8款令人惊叹的HTML5 Canvas动画特效

    HTML5的确可以制作出非常绚丽的网页动画效果,尤其是利用HTML5 Canvas特性和HTML5 3D特性,我们更加可以欣赏到超酷的动画特效.今天我从html5tricks网站上整理了8款令人惊叹的 ...

  10. linux中wget命令

    Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...