-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.sh
executable file
·57 lines (48 loc) · 2.08 KB
/
start.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
#!/bin/sh
set -e
## EMQ Main script
# Start and run emqx, and when emqx crashed, this container will stop
/opt/emqx/bin/emqx start
tail -f /opt/emqx/log/emqx.log.1 &
# Wait and ensure emqx status is running
WAIT_TIME=0
while [[ -z "$(/opt/emqx/bin/emqx_ctl status |grep 'is running'|awk '{print $1}')" ]]
do
sleep 1
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:waiting emqx"
WAIT_TIME=$((WAIT_TIME+1))
if [[ $WAIT_TIME -gt $EMQX_WAIT_TIME ]]; then
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:timeout error"
exit 1
fi
done
# Sleep 5 seconds to wait for the loaded plugins catch up.
sleep 5
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx start"
# monitor emqx is running, or the docker must stop to let docker PaaS know
# warning: never use infinite loops such as `` while true; do sleep 1000; done`` here
# you must let user know emqx crashed and stop this container,
# and docker dispatching system can known and restart this container.
NEW_LOG_NUM=2
IDLE_TIME=0
while [[ $IDLE_TIME -lt 5 ]]
do
IDLE_TIME=$((IDLE_TIME+1))
if [[ ! -z "$(/opt/emqx/bin/emqx_ctl status |grep 'is running'|awk '{print $1}')" ]]; then
IDLE_TIME=0
# Print the latest emqx.log
if [[ -f /opt/emqx/log/emqx.log.${NEW_LOG_NUM} ]];then
tail -f /opt/emqx/log/emqx.log.${NEW_LOG_NUM} &
[[ ! -z $(ps -ef |grep "tail -f /opt/emqx/log/emqx.log" | grep -vE "grep|emqx.log.${NEW_LOG_NUM}" | awk '{print $1}') ]] && kill $(ps -ef |grep "tail -f /opt/emqx/log/emqx.log" | grep -vE "grep|emqx.log.${NEW_LOG_NUM}" | awk '{print $1}')
NEW_LOG_NUM=$((NEW_LOG_NUM+1))
fi
else
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx not running, waiting for recovery in $((25-IDLE_TIME*5)) seconds"
fi
sleep 5
done
# If running to here (the result 5 times not is running, thus in 25s emqx is not running), exit docker image
# Then the high level PaaS, e.g. docker swarm mode, will know and alert, rebanlance this service
# tail $(ls /opt/emqx/log/*)
echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx exit abnormally"
exit 1