Tomcat重启脚本restart.sh停止脚本stop.sh
Tomcat重启脚本restart.sh停止脚本stop.sh
Tomcat本身提供了 startup.sh(启动)shutdown.sh(关闭)脚本,我们在部署中经常会出现死进程形象,无法杀掉进程需要查询进程号kill掉才能正常启动。
通过restart.sh脚本来解决这个问题比较好。
PS:经过后期的使用和验证之前写的脚本还是存在问题,可以看我重新写的一篇文章,参考这个可以比较好的解决问题
完美解决Linux服务器tomcat开机自启动问题 - 流风,飘然的风 - 博客园
http://www.cnblogs.com/zdz8207/p/linux-tomcat-autostartup.html
# 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
----------------------
下面是进过改进版的restart.sh
#!/bin/sh TOMCAT_PATH=/home/server/shichuan/bin echo "TOMCAT_PATH is $TOMCAT_PATH" PID=`ps aux | grep /home/server/shichuan/ | grep java | awk '{print $2}'` if [ -n "$PID" ]; then echo "Will shutdown tomcat: $PID" $TOMCAT_PATH/shutdown.sh -force sleep 5 else echo "No Tomcat Process $PID" fi ps -ef|grep -v grep|grep /home/server/shichuan/ | grep java |awk '{print "kill -9 "$2}'|sh sleep 1 $TOMCAT_PATH/startup.sh sleep 50 for((i=1;i<5;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 50 fi done
----------------------------------------
2.脚本
注意:一台服务器如果运行多个tomcat实例,不要配环境变量,容易启动错误,除了端口变换外,
restart.sh脚本#{tomcat目录}命名要区别,不然kill掉所有tomcat进程
TOMCAT_PATH为tomcat的bin目录,前后不能有反引号
---------------------
restart.sh
---------------------
#!/bin/sh
TOMCAT_PATH=#{tomcat目录}/bin
echo "TOMCAT_PATH is $TOMCAT_PATH"
PID=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'`
if [ -n "$PID" ]; then
echo "Will kill tomcat: $PID"
sh "$TOMCAT_PATH/shutdown.sh"
sleep 6
else echo "No Tomcat Process $PID"
fi
PID2=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'`
if [ -n "$PID2" ]; then
kill -9 $PID2
echo "Try to kill $PID2"
else echo "No Tomcat Process $PID2"
fi
sh "$TOMCAT_PATH/startup.sh"
sleep 3
PID=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'`
if [ -n "$PID" ]; then
echo "\nRestart tomcat successfully!"
else
echo "\nFail to startup tomcat"
exit 1
fi
#!/bin/sh TOMCAT_PATH=#{tomcat目录}/bin echo "TOMCAT_PATH is $TOMCAT_PATH" PID=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'` if [ -n "$PID" ]; then echo "Will kill tomcat: $PID" sh "$TOMCAT_PATH/shutdown.sh" sleep 6 else echo "No Tomcat Process $PID" fi PID2=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'` if [ -n "$PID2" ]; then kill -9 $PID2 echo "Try to kill $PID2" else echo "No Tomcat Process $PID2" fi sh "$TOMCAT_PATH/startup.sh" sleep 3 PID=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'` if [ -n "$PID" ]; then echo "\nRestart tomcat successfully!" else echo "\nFail to startup tomcat" exit 1 fi
===================================
停止脚本stop.sh
-------------------------
#!/bin/sh
TOMCAT_PATH=#{tomcat目录}/bin
echo "TOMCAT_PATH is $TOMCAT_PATH"
PID=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'`
if [ -n "$PID" ]; then
echo "Will kill tomcat: $PID"
sh "$TOMCAT_PATH/shutdown.sh"
sleep 6
else echo "No Tomcat Process $PID"
fi
PID2=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'`
if [ -n "$PID2" ]; then
kill -9 $PID2
echo "Try to kill $PID2"
else echo "No Tomcat Process $PID2"
fi
#!/bin/sh TOMCAT_PATH=#{tomcat目录}/bin echo "TOMCAT_PATH is $TOMCAT_PATH" PID=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'` if [ -n "$PID" ]; then echo "Will kill tomcat: $PID" sh "$TOMCAT_PATH/shutdown.sh" sleep 6 else echo "No Tomcat Process $PID" fi PID2=`ps aux | grep #{tomcat目录} | grep java | awk '{print $2}'` if [ -n "$PID2" ]; then kill -9 $PID2 echo "Try to kill $PID2" else echo "No Tomcat Process $PID2" fi
===================================
创建文件后需要给执行权限
chmod +x restart.sh
chmod +x stop.sh
或者设置成777等权限
重启tomcat时到bin目录下执行./restart.sh 就可以了
===================================
在window本地编辑sh上传可能遇到下面问题,需要解决编码问题
最好的办法是直接使用vi或vim命令创建文件,把内容粘帖进去
错误1.Linux执行.sh文件,提示No such file or directory的问题的解决方法
错误2.bin/sh^M: bad interpreter: No such file or directory解决方法
原因是.sh脚本在windows系统下用记事本文件编写的。不同系统的编码格式引起的。
1.利用如下命令查看文件格式
:set ff 或 :set fileformat
可以看到如下信息
fileformat=dos 或 fileformat=unix
利用如下命令修改文件格式
:set ff=unix 或 :set fileformat=unix
:wq (存盘退出)
2.最好的办法是直接使用vi或vim命令创建文件,把内容粘帖进去
==================================
[root@iZwz96l7gxttha3j1hk494Z bin]# ./restart.sh
./restart.sh: line 1: #!/bin/sh: No such file or directory
./restart.sh: line 2: #{tomcat目录}/bin: is a directory
TOMCAT_PATH is
No Tomcat Process
golang技术交流群:316397059,vuejs技术交流群:458915921 囤币一族:621258209,有兴趣的可以加入
微信公众号: 心禅道(xinchandao)投资论道