linux 查看tomcat状态和日志

来源:互联网 发布:淘宝上好吃的特产 编辑:程序博客网 时间:2024/06/03 23:01

一般服务器部署在linux系统中,

那么在linux 系统中如何查看tomcat日志呢?

 

场景1:浏览器报错了,如何定位错误

查看tomcat 日志的尾部

Shell代码

  1. tail -n 50 ../../logs/catalina.out  

 tail表示只显示catalina.out 最后n 行

 

场景2:如何实时查看tomcat日志内容呢?

Shell代码
  1. tail -f ../../logs/catalina.out  

场景3:通过关键字查询日志

Shell代码
  1. grep -nH "Excetion message" test.text  

说明:grep的参数说明

 -n, --line-number 行号

-H, --with-filename 打印每个匹配的文件名 

-r, --recursive           like --directories=recurse 递归

 

判断tomcat是否在运行

Shell代码
  1. #!/bin/sh  
  2. $grep_result  
  3. grep_result=`ps -ef |grep tomcat|grep "/home/whuang/software/apache/apache-tomcat-7.0.53"|grep -v "grep"`  
  4. echo $grep_result  
  5.     if [ x"$grep_result" = x"" ];then  
  6.         echo "tomcat not run"  
  7.     else  
  8.         echo "tomcat is running..."    
  9.     fi  

定时启动tomcat

编辑定时器:

crontab -e

 

*/1 * * * * /home/whuang/software/auto_start_tomcat.sh

每隔一分钟就执行指定脚本

 

 脚本内容如下:

Shell代码  收藏代码
  1. #!/bin/sh  
  2. $grep_result  
  3. grep_result=`ps -ef |grep tomcat|grep "/home/whuang/software/apache/apache-tomcat-7.0.53"|grep -v "grep"`  
  4. if [ x"$grep_result" = x"" ];then  
  5.   
  6.         catalina_home2=/home/whuang/software/apache/apache-tomcat-7.0.53  
  7.         CATALINA_HOME=$catalina_home2  
  8.         cd $catalina_home2/bin  
  9.         ./startup.sh  
  10.     else  
  11.         echo "tomcat is running..."    
  12.     fi  

 

每天的上午7点30分执行脚本:

30 7 * * *  /home/whuan/software/auto_start_tomcat.sh

 

每天的下午6点执行脚本:

30 18 * * *  /home/whuan/software/auto_innerSign.sh