经过多次测试终于彻底解决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
“|”是管道符,代表将它左边的命令产生的输出,作为标准输出,再作为标准输入,汇入到右边的命令里。| sh 简单来说就是把左边sh文件的内容交给右边sh去执行。==============