Tomcat启动和停止使用shell脚本?

时间:2014-02-12 10:17:25

标签: java linux shell unix tomcat

我有以下shell脚本来停止/停止tomcat。

#!/bin/bash

export BASE=/home/programs/jakarta-tomcat-5.0.28/bin
prog=jakarta-tomcat-5.0.28

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

现在上面的脚本适用于本地tomcat。现在我如何修改上面的脚本来停止/启动远程tomcat

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用ssh在远程计算机上执行本地脚本来启动/停止Tomcat,因此如果您在Linux终端中,可以执行以下操作:

ssh username@remoteMachine /home/username/myScipts/start_tomcat.sh

其中start_tomcat.sh将是远程计算机中的脚本,当然您需要在远程计算机上使用有效的用户名/密码,并且远程计算机还需要安装并运行sshd