如果是系统安装,可以直接用服务管理脚本进行, 如ubuntu在/etc/init.d/tomcat8
主要思路是先用start-stop-daemon发停止信号结tomcat进程, 如果不能顺利结束, 再用kill杀掉进程.
使用方法是
$ sudo service tomcat8 stop
如果你自己用脚本管理,也可以参考这个思路, 贴上停止脚本片段供你参考:
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
set +e
if [ -f "$CATALINA_PID" ]; then
start-stop-daemon --stop --pidfile "$CATALINA_PID" \
--user "$TOMCAT8_USER" \
--retry=TERM/20/KILL/5 >/dev/null
if [ $? -eq 1 ]; then
log_progress_msg "$DESC is not running but pid file exists, cleaning up"
elif [ $? -eq 3 ]; then
PID="`cat $CATALINA_PID`"
log_failure_msg "Failed to stop $NAME (pid $PID)"
exit 1
fi
rm -f "$CATALINA_PID"
rm -rf "$JVM_TMP"
else
log_progress_msg "(not running)"
fi
log_end_msg 0
set -e
;;