linux tomcat 快捷起停脚本

一、安装redhat-lsb

yum install redhat-lsb

进入/usr/local/bin
新建文件tomcat
添加权限
chmod +x tomcat

tomcat

#!/bin/bash

. /etc/profile

if [ -r /lib/lsb/init-functions ]; then
. /lib/lsb/init-functions
else
exit 1
fi

NAME="$(basename $0)"

function usage()
{
echo "Usage: $0 {start|stop|restart} {mkt-web|eshop-app}"
RETVAL="2"
}

if [ $# -ne 2 ]; then
usage
exit 1
fi

APP=$2
if [ -z $APP -o ! -d "/data/web/$APP" ];then
echo "Null parameter or $APP not exists"
exit 1
fi

TOMCAT_HOME="/data/tomcats/$APP"

function start() {
echo "Starting ${APP}: "
if [ "$RETVAL" != "0" ];then
log_failure_msg
return
fi

pid=$(ps aux | grep "${TOMCAT_HOME}" | grep -v "grep" | awk '{print $2}')
if [ -n "$pid" ];then
log_success_msg "is running, PID: $pid"
return
fi

"${TOMCAT_HOME}"/bin/startup.sh
sleep 3
pid=$(ps aux | grep "${TOMCAT_HOME}" | grep -v "grep" | awk '{print $2}')
if [ -z "$pid" ];then
log_failure_msg
RETVAL=1
else
log_success_msg "PID: $pid"
fi
}

function stop() {
echo -n "Stopping ${APP}: "

pid=$(ps aux | grep "${TOMCAT_HOME}" | grep -v grep | awk '{print $2}')
if [ -z "$pid" ];then
echo -n "not running"
log_warning_msg
return
fi

count=0
until [ -z "$(ps aux | grep "${TOMCAT_HOME}" | grep -v grep | awk '{print $2}')" ] || [ $count -gt 3 ];
do
kill -9 $pid
sleep 3
let count="${count}+1"
done
if [ $count -gt 3 ];then
RETVAL=1
log_failure_msg "PID: $pid"
return 1
else
rm -rf /data/tomcats/$APP/work/*
rm -rf /data/tomcats/$APP/temp/*
log_success_msg "PID: $pid"
fi
}

RETVAL="0"
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
usage
;;
esac

exit $RETVAL

 

起停命令:

tomcat start/restart/stop 项目名

原文地址:https://www.cnblogs.com/Nanaya/p/12162130.html