Shell脚本启动/停止tomcat服务器

时间:2019-12-24 08:21:13

标签: bash shell tomcat sh

我正在尝试使用Linux服务器上的shell脚本停止和启动tomcat。 下面是我从Internet复制的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 "$1" 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

1 个答案:

答案 0 :(得分:1)

请按照以下步骤操作:

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

StartStopScript.sh start启动服务器

StartStopScript.sh stop停止服务器

StartStopScript.sh restart重新启动服务器

StartStopScript.sh status检查服务器状态