forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 149
/
run-cmd-tpl.sh
276 lines (256 loc) · 5.51 KB
/
run-cmd-tpl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
#chkconfig - 99 1
#pidfile /home/monitor/monitor_agent/agent.pid
### BEGIN INIT INFO
# Provides: monitor-agent
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: OM monitor agent.
# Description: OM monitor agent.
### END INIT INFO
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
myecho_success() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_SUCCESS
echo -n $" OK "
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 0
}
myecho_failure() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_FAILURE
echo -n $"FAILED"
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
myecho_warning() {
$MOVE_TO_COL
echo -n "["
$SETCOLOR_WARNING
echo -n $"WARNING"
$SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 1
}
mySuccess(){
echo -n $*
myecho_success
echo ""
}
myFailure(){
echo -n $*
myecho_failure
echo ""
}
myWarning(){
echo -n $*
myecho_warning
echo ""
}
myCheckpid() {
local i
for i in $* ; do
[ -d "/proc/$i" ] && return 0
done
return 1
}
myCheckpidfile(){
# return 0 process exists
# return 1 file not exists
# return 2 file exists but process not exists
file=$1
if [ ! -f $file ]; then
return 1
fi
local pid=`cat $file`
if [ -z $pid ]; then
return 1
fi
myCheckpid $pid
ret=$?
if [ $ret -eq 0 ]; then
return 0
else
return 2
fi
}
JAVA_VERSION=1.5
s=`java -version 2>&1`;
i=0;
for e in $s; do
if [ $i -eq 2 ]; then
len=${#e};
len=$(($len-2));
JAVA_VERSION=${e:1:3};
break;
fi;
i=$(($i+1));
done;
JAVA_COMMAND="java"
if [ "$JAVA_VERSION" \> 1.5 ]; then
JAVA_COMMAND="java";
echo "JAVA_VERSION:$JAVA_VERSION";
else
#debian
FIND=0;
JAVA_TMP_HOME="/usr/lib/jvm/java-6-sun"
if [ -e $JAVA_TMP_HOME -a $FIND -eq 0 ]; then
FIND=1;
JAVA_COMMAND="$JAVA_TMP_HOME/bin/java"
echo "debian JAVA_COMMAND: $JAVA_COMMAND";
fi
JAVA_TMP_HOME="/home/monitor/jdk1.6.0_29"
if [ -d $JAVA_TMP_HOME -a $FIND -eq 0 ]; then
FIND=1;
JAVA_COMMAND="$JAVA_TMP_HOME/bin/java"
echo "JAVA_COMMAND: $JAVA_COMMAND";
fi
#redhat
JAVA_TMP_HOME="/usr/java/latest"
if [ -e $JAVA_TMP_HOME -a $FIND -eq 0 ]; then
FIND=1;
JAVA_COMMAND="$JAVA_TMP_HOME/bin/java"
echo "redhat JAVA_COMMAND: $JAVA_COMMAND";
fi
if [ ! -x $JAVA_COMMAND ]; then
echo "$JAVA_COMMAND is not a excutable command!"
exit 1
fi
fi
shellfile=$0
while [ -L $shellfile ]; do
shellfile=`readlink -f $shellfile`
if [ ! -e $shellfile ]; then
echo "$shellfile not exits!"
exit 1
fi
done
#
BASE_HOME=$(cd "$(dirname "$shellfile")"; pwd)
if [ ! -d $BASE_HOME ]; then
echo "$BASE_HOME is not a directory!"
exit 1
fi
#
PID_FILE="/var/lock/sw_monitor.pid"
MONITOR_JAR="$BASE_HOME/sw_monitor.jar"
JARS=`ls $BASE_HOME/*.jar`
for jar in $JARS; do
MONITOR_JAR=$jar
done
MONITOR_APPNAME="SwitchMonitor"
#-Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Dcom.sun.management.jmxremote.port=9312
OPTS="-server -jar -Xmx64m -Xms32m -Dcom.xx.appname=$MONITOR_APPNAME"
COMMAND="$JAVA_COMMAND $OPTS $MONITOR_JAR"
STOP_COMMAND="kill -9"
NAME="OM Switch Monitor"
cd "$BASE_HOME";
echo "BASE_HOME:"`pwd`;
echo "";
start(){
echo $COMMAND
echo -n "start to run $NAME"
myCheckpidfile $PID_FILE
ret=$?
if [ $ret -eq 0 ]; then
echo ""
myWarning "$NAME already exists!"
return 0;
elif [ $ret -eq 1 ] || [ $ret -eq 2 ]; then
$COMMAND > /dev/null 2>&1 &
pid=$!
sleep 2
myCheckpid $pid
ret=$?
if [ $ret -eq 1 ]; then
myFailure
return 1
else
mySuccess
echo "$pid" > $PID_FILE
chmod 666 $PID_FILE > /dev/null 2>&1 || ""
return 0
fi
fi
}
stop(){
echo -n "stopping $NAME"
myCheckpidfile $PID_FILE
ret=$?
if [ $ret -eq 0 ]; then
pid=`cat $PID_FILE`
$STOP_COMMAND $pid >/dev/null
sleep 2
myCheckpid $pid
ret=$?
if [ $ret -eq 1 ]; then
mySuccess
rm -rf $PID_FILE > /dev/null 2>&1 || ""
return 0
else
myFailure
return 1
fi
elif [ $ret -eq 1 ]; then
echo ""
myWarning "$NAME not run"
return 1
else
echo ""
myWarning "$NAME pid file exists but not run"
return 1
fi
}
status(){
myCheckpidfile $PID_FILE
ret=$?
if [ $ret -eq 0 ]; then
pid=`cat $PID_FILE`
mySuccess "$NAME is running with pid=$pid"
exit 0
elif [ $ret -eq 1 ]; then
echo ""
mySuccess "$NAME is not running"
exit 1
else
myWarning "$NAME is not running but pid file exists"
exit 2
fi
}
restart(){
stop
start
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage $0 start|stop|restart|status|dellog"
exit 1;
;;
esac