完美解决Linux服务器tomcat开机自启动问题

经过多次测试终于彻底解决tomcat开机自启动的问题了

PID3=`ps aux | grep /home/server/shichuan/ | grep java | awk '{print $2}'`

if [ -n "$PID3" ]; then
这个判断pid的方式还是可能出现启动失败的情况
# tail -n 1 /home/server/shichuan/logs/catalina.out |grep 'INFO: Server startup in'
INFO: Server startup in 37417 ms
这个才是真正的启动成功,由于是用autostart的方式会先执行停止后启动,一定不会重新最后一行是之前启动成功留下来的
启动成功需要40秒左右,应该先sleep 足够长的时间再检测,否则会出现启动多个tomcat进程的问题
一次性杀掉多个tomcat进程命令:
ps -ef|grep -v grep|grep /home/server/shichuan/ | grep java |awk '{print "kill -9 "$2}'|sh

$TOMCAT_PATH/startup.sh

sleep 60

for((i=1;i<10;i++))
do
LASTINFO=`tail -n 1 /home/server/shichuan/logs/catalina.out |grep 'INFO: Server startup in'`
if [ -n "$LASTINFO" ]; then
break
else
ps -ef|grep -v grep|grep /home/server/shichuan/ | grep java |awk '{print "kill -9 "$2}'|sh
sleep 1
$TOMCAT_PATH/startup.sh
sleep 60
fi
done

设置权限:chmod 777 autostart.sh

在/etc/rc.d/rc.local中加入脚本:
vim /etc/rc.local
/home/server/shichuan/bin/autostart.sh
保存并退出,下次重启会生效。

============================

ps -ef|grep -v grep|grep /home/server/shichuan/ | grep java
ps aux | grep /home/server/shichuan/ | grep java | awk '{print $2}'
ps -ef|grep -v grep|grep /home/server/shichuan/ | grep java |awk '{print "kill -9 "$2}'
ps -ef|grep -v grep|grep /home/server/shichuan/ | grep java |awk '{print "kill -9 "$2}'|sh


ps -ef | 获取当前服务器所有进程
grep -v grep 相当于grep自己吧自己过滤掉,就是不显示grep
grep /home/server/shichuan/ | grep java 过滤出包含这些内容,即过滤出对应的pid
awk 截取 $2 第二个域的pid号。通过调用外部命令kill 来干掉
|sh 把前面print输出的内容作为命令执行

===========================

其他相关命令:

service nginx restart
ls -lh /home/server/shichuan/bin/autostart.sh
cd /home/server/shichuan/bin/

vim /home/server/shichuan/bin/autostart.sh

tail -f /home/server/shichuan/logs/catalina.out

===========================

 下面是autostart.sh的内容:

#!/bin/sh
TOMCAT_PATH=/home/server/shichuan/bin

ps -ef|grep -v grep|grep /home/server/shichuan/ | grep java |awk '{print "kill -9 "$2}'|sh
sleep 10

$TOMCAT_PATH/startup.sh

sleep 60

for((i=1;i<10;i++))
do
    LASTINFO=`tail -n 1 /home/server/shichuan/logs/catalina.out |grep 'INFO: Server startup in'`
    if [ -n "$LASTINFO" ]; then
        break
    else
        ps -ef|grep -v grep|grep /home/server/shichuan/ | grep java |awk '{print "kill -9 "$2}'|sh
        sleep 1
        $TOMCAT_PATH/startup.sh
        sleep 60
    fi
done

 

posted @ 2018-03-08 19:55  爱吃醋的工程师  阅读(202)  评论(0编辑  收藏  举报