问题:tomcat在运行过程中会莫名挂掉,表现如被Killed -9,系统中没有任何日志(命令行无Kill指令记录、/var/log/message中无OOM Killer日志、项目本地目录无core dump日志、无java crash日志、应用中记录了eixt操作,日志中没有看到该记录)。不知道大伙儿遇到过这种情况不,有啥建议不?
下面是我排查的情况:
软件环境
$ uname -a
Linux server-224 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/redhat-release
CentOS release 6.3 (Final)
tomcat 7.0.39
$ java -version
java version "1.7.0_15"
Java(TM) SE Runtime Environment (build 1.7.0_15-b03)
测试流程:
crontab中设置每隔15分不断
重启tomcat
启动
脚本如下:
$ cat notasktest.sh
#!/bin/sh
source /etc/profile
PROJECT="notask"
COUNT=`ps aux|grep java|grep $PROJECT|awk '{print $2}'|wc -l`
if [ $COUNT -le 0 ]
then
echo process not exist
else
cd $
project_dir
bin/shutdown.sh
sleep 5
ps aux|grep "java"|grep $PROJECT|awk '{print $2}'|xargs kill -9
sleep 5
bin/startup.sh
fi
启动成功后的进程信息:
/usr/bin/java -Djava.util.logging.config.file=$project_dir/gameserver.notask/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -Xms4096M -Xmx4096M -XX:PermSize=128M -XX:MaxPermSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:HeapDumpPath=$project_dir/gameserver.notask/log/heap.hprof -Dproject=notask -Djava.rmi.server.hostname=ip -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1108 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:$project_dir/gameserver.notask/log/gc.log -XX:ErrorFile=$project_dir/gameserver.notask/log/java_error_%p.log -Djava.endorsed.dirs=$project_dir/gameserver.notask/endorsed -
classpath $project_dir/gameserver.notask/bin/bootstrap.jar:$project_dir/gameserver.notask/bin/tomcat-juli.jar -Dcatalina.base=$project_dir/gameserver.notask -Dcatalina.home=$project_dir/gameserver.notask -Djava.io.tmpdir=$project_dir/gameserver.notask/temp org.apache.catalina.startup.Bootstrap start
现象:
tomcat进程在启动成功后,可能突然挂掉。效果类似被kill -9
难点:
1、是否挂掉不确定(所以比较难复现,导致很难用回滚法
定位问题所在)
2、挂掉时间不确定,有可能刚启动就挂掉,也可能运行一阵子才挂掉。
3、挂掉后,系统中没有任何日志(tomcat自带的日志
logs下什么都没有,当前项目目录也什么都没有)
分析:
被kill(系统、其他进程、人为、自身)、
异常(系统、自身)
努力:
1 捕捉未处理的异常
Thread.set
DefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
UncaughtExceptionHandler old = Thread
.getDefaultUncaughtExceptionHandler();
@Override
public void uncaughtException(Thread t, Throwable e) {
java.io.StringWriter s = new StringWriter(80);
e.printStackTrace(new PrintWriter(s));
logger.error(
"
线程异常结束:" + t.getName() + "("
+ Long.toHexString(t.getId()) + ")\n"
+ s.toString(), e);
if (old != null) {
old.uncaughtException(t, e);
}
}
});
2 捕捉代码中的exit
System.setSecurityManager(new MySecurityManager());
//------------------MySecurityManager.java关键代码如下
public class MySecurityManager extends SecurityManager {
@Override
public void checkExit(int status) {
logger.error("严重bug,系统退出!"+ new Exception());
throw new SecurityException("not allow to call System.exit");
}
3 在启动指令中(详情见上)指定了XX:+HeapDumpOnOutOfMemoryError XX:HeapDumpPath=$project_dir/gameserver.notask/log/heap.hprof
Xloggc:$project_dir/gameserver.notask/log/gc.log -XX:ErrorFile=$project_dir/gameserver.notask/log/java_error_%p.log
捕捉
堆栈信息、crash信息
4
开启了系统日志
service rsyslog start
5 开启coredump日志
cat /etc/security/limits.conf
* soft nofile 32768
* hard nofile 65536
* soft core unlimited
* hard core unlimited
# End of file
$ ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 62825
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open
files (-n) 32768
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 65535
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
3.6 通过history查看当时没有命令行执行的指令记录,没看到kill指令
export HISTTIMEFORMAT='%F %T - ' #开启每条指令的时间/etc/profile