工作中遇到的问题之tomcat日志切割以及其他碎片整理

昨天,一位同事找我看问题,我发现他们的日志文件特别大,当然在别的公司早已经上了一些日志分析工具的另说,我们还是原始的catalina日志查看。
由于项目组业务量很大,他们当时做的是按照日期去分割,但是后面发现一天的日志下来到晚上已经没法打开了,由于没有关注过这个问题,但是既然有人问我,那我就一定要查出个123来,简单的查了一下,在tomcat的bin目录下vim catalina.sh里面有几句话:

catalina
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Djava.security.manager \
      -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
      | /opt/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null &
else
    "$_RUNJAVA" "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
      | /opt/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null &

个人不懂shell脚本,可是看到这个
/opt/sbin/cronolog “$CATALINA_BASE”/logs/catalina.%Y-%m-%d.out
非常熟悉,既然这里%Y-%m-%d,格式化的输出了时间,肯定前面有定义。
在往上面看,发现有这样的代码:

JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$CATALINA_HOME/logs/oom_java_heapDum$(date +%Y%m%d%H%M%S).hprof"

他可以用H当小时,我也试一下,尝试了一下,发现的确按照小时进行了日志分割。
修改一下上面的那两段代码

/opt/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null
修改为:
/opt/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d-%H.out >> /dev/null

然后重启tomcat就好了。
其实这个问题是治标不治本,我们在检索日志的时候文件大的时候定位问题很困难,如果有机会做一个基于es的信息检索系统用于日志分析,应该可以提高定位和解决问题的效率。

其他一些问题碎片

1、sql删除问题
前两天有个同事,要解决一个SQL的问题,即删除一些记录,这些记录需要通过连表查询。找了个stackoverflow里的answer作为举例吧。下述两种方式都可以用于删除表PRODUCTFILTERS 中的记录

delete from
(
select pf.* From PRODUCTFILTERS pf 
where pf.id>=200 
And pf.rowid in 
  (
     Select rowid from PRODUCTFILTERS 
     inner join PRODUCTS on PRODUCTFILTERS.PRODUCTID = PRODUCTS.ID 
     And PRODUCTS.NAME= 'Mark'
  )
); 
delete from PRODUCTFILTERS where rowid in
(
select pf.rowid From PRODUCTFILTERS pf 
where pf.id>=200 
And pf.rowid in 
  (
     Select PRODUCTFILTERS.rowid from PRODUCTFILTERS 
     inner join PRODUCTS on PRODUCTFILTERS.PRODUCTID = PRODUCTS.ID 
     And PRODUCTS.NAME= 'Mark'
  )
); 

但是下面的方法在oracle中是不适用的,虽然很简洁,但是可以用于sql server中。

DELETE BT FROM JBPM_BYTEARRAY BT 
          INNER JOIN JBPM_VARIABLEINSTANCE JVST 
          ON BT.ID_ = JVST.BYTEARRAYVALUE_
             WHERE JVST.PROCESSINSTANCE_ IN (10071713859, 10071713920);

2、查看当前运行程序端口占用情况
比如说你要看哪个端口被占用可以用如下命令:

netstat -tulpn |grep :80

tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      26417/java          
tcp        0      0 :::8009                     :::*                        LISTEN      26417/java          
tcp        0      0 :::8090                     :::*                        LISTEN      26417/java          

netstat net status 表示这个命令是查看网络状态的。
-tulpn 分别表示 t: tcp u:udp l:listening p:programs n:numeric
| 表示管道,grep是查询命令是指查询含有’:80’的字符串。

3、linux下常用的命令快捷键

Command Editing Shortcuts

Ctrl + a – go to the start of the command line
Ctrl + e – go to the end of the command line
Ctrl + k – delete from cursor to the end of the command line
Ctrl + u – delete from cursor to the start of the command line
Ctrl + w – delete from cursor to start of word (i.e. delete backwards one word)
Ctrl + y – paste word or text that was cut using one of the deletion shortcuts (such as the one above) after the cursor
Ctrl + xx – move between start of command line and current cursor position (and back again)
Alt + b – move backward one word (or go to start of word the cursor is currently on)
Alt + f – move forward one word (or go to end of word the cursor is currently on)
Alt + d – delete to end of word starting at cursor (whole word if cursor is at the beginning of word)
Alt + c – capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word)
Alt + u – make uppercase from cursor to end of word
Alt + l – make lowercase from cursor to end of word
Alt + t – swap current word with previous
Ctrl + f – move forward one character
Ctrl + b – move backward one character
Ctrl + d – delete character under the cursor
Ctrl + h – delete character before the cursor
Ctrl + t – swap character under cursor with the previous one

上述命令,我没有在linux环境中测试过,但在ssh工具中使用过 Ctrl + a Ctrl + e Ctrl + k Ctrl + w Ctrl + u

好了,今天就写这么多吧。
总是做一些杂事,心里好烦。TMD

参考文献:
http://stackoverflow.com/questions/12672082/delete-with-join-in-oracle-sql-query
http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/

你可能感兴趣的:(工作经验,tomcat,工作)