读取INPUT_STRING 案例$ INPUT_STRING
stop)
check=`netstat -na | grep 8080 | awk '{print $6}'|head -1`
if [ "$check" = "TIME_WAIT" ]; then
echo "DEFAULT TOMCAT PORT IS LISTENING, SO ITS stopped already..."
else
sh /opt/apache-tomcat-8.0.39/bin/shutdown.sh > /dev/null 2>&1
echo "Tomcat server is stopped....."
fi
这里我的代码是找到仅在8080端口上运行的tomcat服务器,以便很容易找到tomcat是否正在运行。但实际上我需要的是停止/启动tomcat服务器,如果它正在运行或在其他一些端口中停止状态......
答案 0 :(得分:0)
创建一个正在运行tomcat的pids数组,并检查每一个:
declare -a list
list=( `ps -AChS | grep '[t]omcat' | awk '{ print $1 }'`)
for i in ${list[*]} ;
do netstat -vlp tcp | awk '{ print $6, $9 }' | grep "$i" ;
done
unset "$list"
我在这里创建一个tomcat pids数组,然后使用grep查找每个pid的网络状态。一旦我完成了循环,我就清除了阵列。注意:netstat在各种操作系统上有不同的实现,在我的例子中,我使用的是verbose,它显示了进程的pid和tcp的过滤。
使用catalina.sh启动或停止服务,例如catalina.sh stop
答案 1 :(得分:0)
# This script is works based on tomcat server is running or stopped status. if it is stopped I need to restart it by giving option as start otherwise stop based on the requirement....
#!/bin/sh
echo "Choose Your Option---- status or stop.."
echo
echo "Enter Your Option ..."
path=`whereis -l tomcat | grep tomcat | sed 's/bin: //' |head -1`
echo $path
echo "Enter Your Option ..."
while :
do
read INPUT_STRING
case $INPUT_STRING in
stop)
check=` ps -aef |grep java| grep tomcat |awk '{print $2}'`
if [ -z "$check" ]; then
echo "Tomcat server is stopped already..."
else
sh $path/shutdown.sh > /dev/null 2>&1
echo "The Tomcat server is stopped successfully..."
fi
exit;
;;
start)
check=`ps -aef |grep java| grep tomcat |awk '{print $2}'`
if [ ! -z ${check} ]; then
echo "Tomcat server is already running successfully..."
else
sh $path/startup.sh > /dev/null 2>&1
echo "Tomcat server is started....."
fi
exit;
;;
status)
check=`ps -aef |grep java| grep tomcat |awk '{print $2}'`
if [ ! -z ${check} ]; then
echo "Tomcat server is already running..."
else
echo "Tomcat server is stopped...."
fi
exit;
;;
esac
done