start.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/sh
  2. set -e -u
  3. EMQX_WAIT_TIME=${EMQX_WAIT_TIME:-5}
  4. emqx_exit(){
  5. # At least erlang.log.1 exists
  6. if [ -f /opt/emqx/log/erlang.log.1 ]; then
  7. # tail emqx.log.*
  8. erlang_log=$(echo $(ls -t /opt/emqx/log/erlang.log.*) | awk '{print $1}')
  9. num=$(sed -n -e '/LOGGING STARTED/=' ${erlang_log} | tail -1)
  10. [ ! -z $num ] && [ $num -gt 2 ] && tail -n +$(expr $num - 2) ${erlang_log}
  11. fi
  12. echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx exit abnormally"
  13. exit 1
  14. }
  15. ## EMQ Main script
  16. # When receiving the EXIT signal, execute emqx_exit function
  17. trap "emqx_exit" EXIT
  18. # Start and run emqx, and when emqx crashed, this container will stop
  19. /opt/emqx/bin/emqx start
  20. # Sleep 5 seconds to wait for the loaded plugins catch up.
  21. sleep 5
  22. echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx start"
  23. ## Fork tailing erlang.log, the fork is not killed after this script exits
  24. ## The assumption is that this is the docker entrypoint,
  25. ## hence docker container is terminated after entrypoint exists
  26. tail -f /opt/emqx/log/erlang.log.1 &
  27. # monitor emqx is running, or the docker must stop to let docker PaaS know
  28. # warning: never use infinite loops such as `` while true; do sleep 1000; done`` here
  29. # you must let user know emqx crashed and stop this container,
  30. # and docker dispatching system can known and restart this container.
  31. IDLE_TIME=0
  32. MGMT_CONF='/opt/emqx/etc/plugins/emqx_management.conf'
  33. MGMT_PORT=$(sed -n -r '/^management.listener.http[ \t]=[ \t].*$/p' $MGMT_CONF | sed -r 's/^management.listener.http = (.*)$/\1/g')
  34. while [ $IDLE_TIME -lt 5 ]; do
  35. IDLE_TIME=$(expr $IDLE_TIME + 1)
  36. if curl http://localhost:${MGMT_PORT}/status >/dev/null 2>&1; then
  37. IDLE_TIME=0
  38. # Print the latest erlang.log
  39. now_erlang_log=$(ps -ef |grep "tail -f /opt/emqx/log/erlang.log" |grep -v grep | sed -r "s/.*tail -f (.*)/\1/g")
  40. new_erlang_log="$(ls -t /opt/emqx/log/erlang.log.* | head -1)"
  41. if [ $now_erlang_log != $new_erlang_log ];then
  42. tail -f $new_erlang_log &
  43. kill $(ps -ef |grep "tail -f $now_erlang_log" | grep -v grep | awk '{print $1}')
  44. fi
  45. else
  46. echo "['$(date -u +"%Y-%m-%dT%H:%M:%SZ")']:emqx not running, waiting for recovery in $((25-IDLE_TIME*5)) seconds"
  47. fi
  48. sleep $EMQX_WAIT_TIME
  49. done
  50. # If running to here (the result 5 times not is running, thus in 25s emqx is not running), exit docker image
  51. # Then the high level PaaS, e.g. docker swarm mode, will know and alert, rebanlance this service