tomcat添加为service服务-基于Debian 9

前置条件

正确配置jdk和tomcat

在/etc/init.d/文件夹下创建tomcat文件,内容如下

 1 #!/bin/sh
 2 RETVAL=0
 3 
 4 #此处需要加入自己系统的实际Java环境和catalina路径
 5 export JAVA_HOME=/usr/local/java
 6 export JRE_HOME=/usr/local/java
 7 export CATALINA_HOME=/usr/local/tomcat
 8 export CATALINA_BASE=/usr/local/tomcat
 9 
10 start()
11 {
12         if [ -f $CATALINA_HOME/bin/startup.sh ];
13           then
14             echo $"Starting Tomcat"
15                 $CATALINA_HOME/bin/startup.sh
16             RETVAL=$?
17             echo " OK"
18             return $RETVAL
19         fi
20 }
21 stop()
22 {
23         if [ -f $CATALINA_HOME/bin/shutdown.sh ];
24           then
25             echo $"Stopping Tomcat"
26                 $CATALINA_HOME/bin/shutdown.sh
27             RETVAL=$?
28             sleep 1
29             ps -fwwu root | grep tomcat|grep -v grep | grep -v PID | awk '{print $2}'|xargs kill -9
30             echo " OK"
31             # [ $RETVAL -eq 0 ] && rm -f /var/lock/...
32             return $RETVAL
33         fi
34 }
35 
36 case "$1" in
37  start)
38         start
39         ;;
40  stop)
41         stop
42         ;;
43 
44  restart)
45          echo $"Restaring Tomcat"
46          $0 stop
47          sleep 1
48          $0 start
49          ;;
50 status)
51         ps -ef | grep tomcat | grep bootstrap.jar |grep start  >>null
52         if [ $? -ne 0 ]
53         then
54          echo "tomcat stoped"
55         else
56          ps -ef | grep tomcat | grep bootstrap.jar |grep start | awk '{print "tomcat pid: "$2}'
57          echo "tomcat is runing....."
58         fi
59         ;;
60 
61  *)
62         echo $"Usage: $0 {start|stop|restart|status}"
63         exit 1
64         ;;
65 esac
66 exit $RETVAL

执行如下命令

update-rc.d tomcat defaults

验证

service tomcat status/start/stop

posted @ 2018-11-21 10:30  刘笙  阅读(1629)  评论(0编辑  收藏  举报