您的位置:首页 > 运维架构 > Shell

实现tomcat自动启动shell编程

2019-06-10 08:54 148 查看
#!/bin/bash
#
# tomcat
#
# chkconfig: 2345 88 86
# description: tomcat server. \
#              This service starts up the OpenSSH server daemon.
#
. /etc/rc.d/init.d/functions

tomcat_home="/root/tomcat"
#为了判断tomcat是否已经启动了,我们需要把tomcat启动之后的进程ID保存起来.后面可以根据该进程ID来判断是否启动
pid_file=/tmp/tomcat.pid

#判断tomcat是否启动的函数
tomcat_status(){
if [ -f $pid_file ];then  #tomcat的进程ID保存到pid_file中,判断该文件存在
PID=`cat $pid_file`   # 从文件中读取进程ID赋值给本地变量PID
if [ ! -s $PID ];then   # 判断本地变量PID是否不为空
checkpid $PID   #检查当前tomcat进程是否正在运行
return $?
fi
fi
return 1
}

#tomcat启动的函数
tomcat_start(){
#pid文件可能不存在,如果不存在,需要使用touch命令创建该pid文件
if [ ! -f $pid_file ];then
touch $pid_file
fi
#如果当前tomcat没有启动,我们才启动tomcat
tomcat_status
if [ $? -eq 0 ];then
echo "tomcat已经启动,不需要再次启动"
return 0
fi

#执行tomcat的startup.sh,来启动tomcat
if [ -x "$tomcat_home/bin/startup.sh" ];then
#执行该startup.sh文件,如果该文件执行之后能够把tomcat进程打印出来,那获取pid容易多了
$tomcat_home/bin/startup.sh | grep "^Tomcat started" | cut -d"=" -f2 >$pid_file
fi
}

#定义tomcat停止的函数
tomcat_stop(){
#停止tomcat通过执行shutdown.sh来完成 ,停止之前要判断一下该tomcat是否已经启动
tomcat_status
if [ $? -eq 0 ];then
if [ -x "$tomcat_home/bin/shutdown.sh" ];then
$tomcat_home/bin/shutdown.sh &>/dev/null
return $?
else
echo "该用户没有执行的权限,请检查"
fi
else
echo "tomcat没有启动,不需要停止"
return 0
fi
}

case $1 in
"start")
#执行启动的语句
tomcat_start
if [ $? -eq 0 ];then
tomcat_status && echo "tomcat已经启动成功"
else
echo "tomcat启动出错,请检查"
fi

;;
"stop")
#执行停止tomcat的语句
tomcat_stop
if [ $? -eq 0 ];then
echo "tomcat已经停止..."
fi
;;
"restart")
#执行重启tomcat的语句
tomcat_stop
sleep 1
tomcat_start
;;
"status")
#执行查看tomcat服务器状态的语句
tomcat_status
if [ $? -eq 0 ];then
echo "tomcat正在运行...."
else
echo "tomcat没有启动"
fi
;;
*)
echo "传入参数错误"
echo "参数只能是:tomcat {start|stop|restart|status} 其中一个"
exit 2
;;
esac
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: