1 #!/bin/bash
2
3 # Apache Tomcat daemon
4 #
5 # chkconfig: 345 10 10
6 # description: Apache Tomcat daemon
7 #
8 # processname: tomcat
9
10 #define variables
11 tom="/usr/local/tomcat"
12 startup_bin="bin/startup.sh"
13 shutdown_bin="bin/shutdown.sh"
14 usage="{1|2|3|all} {start|stop|restart|status}"
15 dev="/dev/null"
16 command="restart"
17
18 #judge $1 $2 whether null
19 if [ "$1" == "" ];then
20 echo "Usage: $0 $usage"
21 echo "Usage: $0 {start|stop|restart|status}"
22 exit 1
23 elif [ "$2" == "" ];then
24 tomcats="1 2 3"
25 command=$1
26 else
27 tomcats=$1
28 command=$2
29 fi
30
31 #judge $1
32 if [[ $2 != "" ]];then
33 case $1 in
34 "1")
35 tomcats="1"
36 ;;
37
38 "2")
39 tomcats="2"
40 ;;
41
42 "3")
43 tomcats="3"
44 ;;
45
46 "all")
47 tomcats="1 2 3"
48 ;;
49
50 *)
51 echo "Usage: $0 $usage"
52 echo "Usage: $0 {start|stop|restart|status}"
53 exit 1
54 ;;
55 #echo "Usage: $0 $usage"
56 #;;
57 esac
58 else
59 tomcats="1 2 3"
60 fi
61
62 #define start function
63 tomcatstart() {
64 for i in $tomcats
65 do
66 tom_home="$tom$i"
67 run_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom_home}")
68 if [ "${run_status}X" != "X" ];then
69 echo "tomcat $i is already running..."
70 else
71 ${tom_home}/${startup_bin} &>$dev
72 echo "tomcat $i starting,Please wait 2s..."
73 sleep 2
74 fi
75 done
76 }
77
78 #define stop function
79 tomcatstop() {
80 for j in $tomcats
81 do
82 tom1_home="$tom$j"
83 run1_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom1_home}")
84 if [ "${run1_status}X" == "X" ];then
85 echo "tomcat $j is not running..."
86 else
87 ${tom1_home}/${shutdown_bin} &>$dev
88 echo "tomcat $j stopping,Please wait 2s..."
89 sleep 2
90 fi
91 done
92 }
93
94 #define restart function
95 tomcatrestart() {
96 for m in $tomcats
97 do
98 tom2_home="$tom$m"
99 run2_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom2_home}")
100 if [ "${run2_status}X" == "X" ];then
101 echo "tomcat $m is not running..."
102 ${tom2_home}/${startup_bin} &>$dev
103 echo "tomcat $m starting,Please wait 2s..."
104 sleep 2
105 else
106 ${tom2_home}/${shutdown_bin} &>$dev
107 echo "tomcat $m stopping,Please wait 2s..."
108 sleep 2
109 ${tom2_home}/${startup_bin} &>$dev
110 echo "tomcat $m starting,Please wait 2s..."
111 sleep 2
112 fi
113 done
114 }
115
116 #define status function
117 tomcatstatus() {
118 for n in $tomcats
119 do
120 tom3_home="$tom$n"
121 run3_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom3_home}")
122 if [ "${run3_status}X" == "X" ];then
123 echo "tomcat $n is not running..."
124 else
125 echo "tomcat $n is running"
126 fi
127 done
128 }
129
130 #judge $command
131 case $command in
132 "start")
133 tomcatstart
134 ;;
135
136 "stop")
137 tomcatstop
138 ;;
139
140 "restart")
141 tomcatrestart
142 ;;
143
144 "status")
145 tomcatstatus
146 ;;
147 *)
148 echo "Usage: $0 $usage"
149 ;;
150 esac