shell 到 Start/Stop tomcat 服务器的脚本

shell script to Start/Stop tomcat server

我正在尝试在 linux 服务器上使用 shell 脚本停止和启动 tomcat。 下面是我从互联网上复制的 shell 脚本,但它不起作用。我是 shell 脚本的新手。 请任何人都可以帮助我。提前致谢。

#!/bin/bash

export BASE=/opt/tomcat/apache-tomcat-8.5.47/bin
prog=apache-tomcat-8.5.47

stat() {
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
echo Tomcat is running.
else
echo Tomcat is not running.
fi
}

case "" in
start)

if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
echo Tomcat seems to be running. Use the restart option.
else
$BASE/startup.sh 2>&1 > /dev/null
fi
stat
;;
stop)
$BASE/shutdown.sh 2>&1 > /dev/null
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
for pid in `ps auxwww|grep $prog|grep -v grep|tr -s ' '|cut -d ' ' -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
stat
;;
restart)

if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
for pid in `ps auxwww|grep $prog|grep -v grep|tr -s ' '|cut -d ' ' -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
$BASE/startup.sh 2>&1 > /dev/null
stat
;;
status)
stat
;;
*)
echo "Usage: tomcat start|stop|restart|status"
esac

请按照以下步骤操作:

  • 将提供的代码保存在 .sh 文件(即 StartStopScript.sh)的一个位置。
  • 用你更新 export BASE 变量 tomcat bin 位置。
  • 用 tomcat 版本更新 prog 变量。
  • 重要提示: 运行 带有参数的脚本,例如。

StartStopScript.sh start 启动服务器

StartStopScript.sh stop 停止服务器

StartStopScript.sh restart 重启服务器

StartStopScript.sh status 检查服务器状态