start.sh 2.4 KB

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