tomcat启动脚本
1 #!/bin/bash
2 #
3 # chkconfig: - 95 15
4 # description: Tomcat start/stop/status script
5
6 #Location of JAVA_HOME (bin files)
7 export JAVA_HOME=/usr/local/jdk1.7
8
9 #Add Java binary files to PATH
10 export PATH=$JAVA_HOME/bin:$PATH
11
12 #CATALINA_HOME is the location of the configuration files of this instance of Tomcat
13 CATALINA_HOME=/usr/tomcats/tomcat8081
14
15 #TOMCAT_USER is the default user of tomcat
16 TOMCAT_USER=root
17
18 #TOMCAT_USAGE is the message if this script is called without any options
19 TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
20
21 #SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
22 SHUTDOWN_WAIT=20
23
24 tomcat_pid() {
25 echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s " "|cut -d" " -f2`
26 }
27
28 start() {
29 pid=$(tomcat_pid)
30 if [ -n "$pid" ];then
31 echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
32 else
33 echo -e "\e[00;32mStarting tomcat\e[00m"
34 if [ `user_exists $TOMCAT_USER` = "1" ];then
35 su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
36 else
37 $CATALINA_HOME/bin/startup.sh
38 fi
39 status
40 fi
41 return 0
42 }
43
44 status(){
45 pid=$(tomcat_pid)
46 if [ -n "$pid" ];then
47 echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
48 else
49 echo -e "\e[00;31mTomcat is not running\e[00m"
50 fi
51 }
52
53 stop() {
54 pid=$(tomcat_pid)
55 if [ -n "$pid" ];then
56 echo -e "\e[00;31mStoping Tomcat\e[00m"
57 $CATALINA_HOME/bin/shutdown.sh
58
59 let kwait=$SHUTDOWN_WAIT
60 count=0;
61 until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
62 do
63 echo -n -e "\e[00;31mwaiting for processes to exit\e[00m\n";
64 sleep 1
65 let count=$count+1;
66 done
67
68 if [ $count -gt $kwait ];then
69 echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
70 kill -9 $pid
71 fi
72 else
73 echo -e "\e[00;31mTomcat is not running\e[00m"
74 fi
75
76 return 0
77 }
78
79 user_exists(){
80 if id -u $1 >/dev/null 2>&1; then
81 echo "1"
82 else
83 echo "0"
84 fi
85 }
86
87 case $1 in
88 start)
89 start
90 ;;
91
92 stop)
93 stop
94 ;;
95
96 restart)
97 stop
98 start
99 ;;
100
101 status)
102 status
103 ;;
104
105 *)
106 echo -e $TOMCAT_USAGE
107 ;;
108 esac
109 exit 0
说明:红色字体是需要根据自己的tomcat以及jdk的路径进行手动配置的,运行tomcat的用户也需要自己配置,配置的账号系统中要存在。