vi server.sh
#!/bin/bash export JAVA_HOME=/u01/java_home/jdk1.8.0_181 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH APP_HOME=`pwd` if [[ $# != 2 ]]; then echo "Usage: $0 {start|stop|restart} {tomcat1|tomcat2|...}" >&2 exit fi PROJECT_DIR="${APP_HOME}/$2" case $1 in start) #check tomcat if [[ ! -f "${PROJECT_DIR}/bin/startup.sh" ]]; then echo "tomcat startup.sh not exist. path:${PROJECT_DIR}/bin" exit fi if [[ ! -x "${PROJECT_DIR}/bin/startup.sh" ]]; then echo "tomcat file startup.sh is not a executable file" exit fi PID=$(ps -ef|grep ${PROJECT_DIR}|grep -v grep|awk '{print $2}') if test -n "${PID}"; then echo "server already running as process:${PID}" exit 1 fi #start project echo "$2 starting..." sh ${PROJECT_DIR}/bin/startup.sh &>/dev/null if [[ $? -eq 0 ]]; then sleep 1 echo "$2 started" else echo "$2 start fail" exit 1 fi ;; stop) echo "$2 stopping..." PID=$(ps -ef|grep ${PROJECT_DIR}|grep -v grep|awk '{print $2}') if test -z "${PID}"; then echo "$2 not started" else kill -15 ${PID} if [[ $? -eq 0 ]]; then sleep 1 echo "$2 stopped" else echo "$2 stop fail" exit 1 fi fi ;; restart) "$0" stop $2 sleep 5 "$0" start $2 ;; *) echo "Usage: $0 {start|stop|restart} {tomcat1|tomcat2|...}" >&2 ;; esac