Linux系统Tomcat服务自启动脚本

Linux系统Tomcat服务自启动脚本

在linux系统下,手动寻找tomcat的bin目录下的 startup.sh 及 shutdown.sh 文件往往需要花费较长的时间!!

本文提供一种将tomcat设置为linux系统服务的方式, 可以设置tomcat 与 iptables 等其他系统服务一样随开机启动,并提供 start stop  restarts  status等命令,便于操作.

前提条件!!   正确安装JDK 并已正确设置好环境变量 .

( 1 )  在/etc/init.d目录下 创建  tomcat 服务

( 2 ) vi 编辑新添加的tomcat文件

  1 #!/bin/bash
  2 #
  3 # tomcat
  4 # 
  5 
  6 #   --level<等级代号>  指定读系统服务要在哪一个执行等级中开启或关毕。
  7 #      等级0表示:表示关机
  8 #     等级1表示:单用户模式
  9 #      等级2表示:无网络连接的多用户命令行模式
 10 #      等级3表示:有网络连接的多用户命令行模式
 11 #      等级4表示:不可用
 12 #      等级5表示:带图形界面的多用户模式
 13 #      等级6表示:重新启动
 14 
 15 # chkconfig: 2345 88 86
 16 # description: tomcat server. \
 17 #              This service starts up the OpenSSH server daemon.
 18 #
 19 
 20 
 21 
 22 #对传入参数的数量进行判断
 23 [$# ne 1] && echo "传入参数错误!参数只能是:tomcat {start|stop|restart|status} 其中一个" && exit 2
 24 
 25 #导入库函数
 26 . /etc/rc.d/init.d/functions
 27 tomcat_home="/root/tomcat"
 28 #为了判断tomcat是否已经启动了,我们需要把tomcat启动之后的进程ID保存起来.后面可以根据该进程ID来判断是否启动
 29 pid_file=/tmp/tomcat.pid
 30 #判断tomcat是否启动的函数
 31 tomcat_status(){
 32         if [ -f $pid_file ];then  #tomcat的进程ID保存到pid_file中,判断该文件存在
 33                 PID=`cat $pid_file`   # 从文件中读取进程ID赋值给本地变量PID
 34                 if [ ! -s $PID ];then   # 判断本地变量PID是否不为空
 35                         checkpid $PID   #检查当前tomcat进程是否正在运行
 36                         return $?        
 37                 fi    
 38         fi
 39         return 1
 40 }
 41 #tomcat启动的函数
 42 tomcat_start(){
 43         #pid文件可能不存在,如果不存在,需要使用touch命令创建该pid文件
 44         if [ ! -f $pid_file ];then
 45                 touch $pid_file
 46         fi
 47         #如果当前tomcat没有启动,我们才启动tomcat
 48         tomcat_status
 49         if [ $? -eq 0 ];then
 50                 echo "tomcat已经启动,不需要再次启动"
 51                 return 0
 52         fi
 53         
 54         #执行tomcat的startup.sh,来启动tomcat
 55         if [ -x "$tomcat_home/bin/startup.sh" ];then
 56                 #执行该startup.sh文件,如果该文件执行之后能够把tomcat进程打印出来,那获取pid容易多了
 57                 $tomcat_home/bin/startup.sh | grep "^Tomcat started" | cut -d"=" -f2 >$pid_file
 58         fi
 59 }
 60 #定义tomcat停止的函数
 61 tomcat_stop(){
 62         #停止tomcat通过执行shutdown.sh来完成 ,停止之前要判断一下该tomcat是否已经启动
 63         tomcat_status
 64         if [ $? -eq 0 ];then
 65                 if [ -x "$tomcat_home/bin/shutdown.sh" ];then
 66                         $tomcat_home/bin/shutdown.sh &>/dev/null
 67                         return $?
 68                 else
 69                         echo "该用户没有执行的权限,请检查"
 70                 fi
 71         else
 72                 echo "tomcat没有启动,不需要停止"
 73                 return 0
 74         fi
 75 }
 76 case $1 in
 77 "start")
 78         #执行启动的语句
 79         tomcat_start
 80         if [ $? -eq 0 ];then
 81                 tomcat_status && echo "tomcat已经启动成功"
 82         else
 83                 echo "tomcat启动出错,请检查"
 84         fi
 85         
 86         ;;
 87 "stop")
 88         #执行停止tomcat的语句
 89         tomcat_stop
 90         if [ $? -eq 0 ];then
 91                 echo "tomcat已经停止..."
 92         fi
 93         ;;
 94 "restart")
 95     #执行重启tomcat的语句
 96         tomcat_stop
 97         sleep 1
 98         tomcat_start
 99     ;;
100 "status")
101     #执行查看tomcat服务器状态的语句
102         tomcat_status
103         if [ $? -eq 0 ];then
104                 echo "tomcat正在运行...."
105         else
106                 echo "tomcat没有启动"
107         fi
108     ;;
109 *)
110         echo "传入参数错误"
111         echo "参数只能是:tomcat {start|stop|restart|status} 其中一个"
112         exit 2
113         ;;
114 esac

( 3 ) 对tomcat文件设置权限  ( 一定要记得添加执行权限,其他权限视个人而定)

 chmod +x  tomcat

猜你喜欢

转载自www.cnblogs.com/eric666666/p/10994777.html