app.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/bash
  2. set -e
  3. source /etc/profile
  4. export JAVA_HOME=/usr/java/latest
  5. export PATH=$JAVA_HOME/bin:$PATH
  6. touch /tmp/start.log
  7. chown admin: /tmp/start.log
  8. chown -R admin: /home/admin/canal-server
  9. host=`hostname -i`
  10. # waitterm
  11. # wait TERM/INT signal.
  12. # see: http://veithen.github.io/2014/11/16/sigterm-propagation.html
  13. waitterm() {
  14. local PID
  15. # any process to block
  16. tail -f /dev/null &
  17. PID="$!"
  18. # setup trap, could do nothing, or just kill the blocker
  19. trap "kill -TERM ${PID}" TERM INT
  20. # wait for signal, ignore wait exit code
  21. wait "${PID}" || true
  22. # clear trap
  23. trap - TERM INT
  24. # wait blocker, ignore blocker exit code
  25. wait "${PID}" 2>/dev/null || true
  26. }
  27. # waittermpid "${PIDFILE}".
  28. # monitor process by pidfile && wait TERM/INT signal.
  29. # if the process disappeared, return 1, means exit with ERROR.
  30. # if TERM or INT signal received, return 0, means OK to exit.
  31. waittermpid() {
  32. local PIDFILE PID do_run error
  33. PIDFILE="${1?}"
  34. do_run=true
  35. error=0
  36. trap "do_run=false" TERM INT
  37. while "${do_run}" ; do
  38. PID="$(cat "${PIDFILE}")"
  39. if ! ps -p "${PID}" >/dev/null 2>&1 ; then
  40. do_run=false
  41. error=1
  42. else
  43. sleep 1
  44. fi
  45. done
  46. trap - TERM INT
  47. return "${error}"
  48. }
  49. function checkStart() {
  50. local name=$1
  51. local cmd=$2
  52. local timeout=$3
  53. cost=5
  54. while [ $timeout -gt 0 ]; do
  55. ST=`eval $cmd`
  56. if [ "$ST" == "0" ]; then
  57. sleep 1
  58. let timeout=timeout-1
  59. let cost=cost+1
  60. elif [ "$ST" == "" ]; then
  61. sleep 1
  62. let timeout=timeout-1
  63. let cost=cost+1
  64. else
  65. break
  66. fi
  67. done
  68. echo "start $name successful"
  69. }
  70. function start_canal() {
  71. echo "start canal ..."
  72. managerAddress=`perl -le 'print $ENV{"canal.admin.manager"}'`
  73. if [ ! -z "$managerAddress" ] ; then
  74. # canal_local.properties mode
  75. adminPort=`perl -le 'print $ENV{"canal.admin.port"}'`
  76. if [ -z "$adminPort" ] ; then
  77. adminPort=11110
  78. fi
  79. su admin -c 'cd /home/admin/canal-server/bin/ && sh restart.sh local 1>>/tmp/start.log 2>&1'
  80. sleep 5
  81. #check start
  82. checkStart "canal" "nc 127.0.0.1 $adminPort -w 1 -z | wc -l" 30
  83. else
  84. metricsPort=`perl -le 'print $ENV{"canal.metrics.pull.port"}'`
  85. if [ -z "$metricsPort" ] ; then
  86. metricsPort=11112
  87. fi
  88. destination=`perl -le 'print $ENV{"canal.destinations"}'`
  89. if [[ "$destination" =~ ',' ]]; then
  90. echo "multi destination:$destination is not support"
  91. exit 1;
  92. else
  93. if [ "$destination" != "" ] && [ "$destination" != "example" ] ; then
  94. if [ -d /home/admin/canal-server/conf/example ]; then
  95. mv /home/admin/canal-server/conf/example /home/admin/canal-server/conf/$destination
  96. fi
  97. fi
  98. fi
  99. su admin -c 'cd /home/admin/canal-server/bin/ && sh restart.sh 1>>/tmp/start.log 2>&1'
  100. sleep 5
  101. #check start
  102. checkStart "canal" "nc 127.0.0.1 $metricsPort -w 1 -z | wc -l" 30
  103. fi
  104. }
  105. function stop_canal() {
  106. echo "stop canal"
  107. su admin -c 'cd /home/admin/canal-server/bin/ && sh stop.sh 1>>/tmp/start.log 2>&1'
  108. echo "stop canal successful ..."
  109. }
  110. function start_exporter() {
  111. su admin -c 'cd /home/admin/node_exporter && ./node_exporter 1>>/tmp/start.log 2>&1 &'
  112. }
  113. function stop_exporter() {
  114. su admin -c 'killall node_exporter'
  115. }
  116. echo "==> START ..."
  117. start_exporter
  118. start_canal
  119. echo "==> START SUCCESSFUL ..."
  120. tail -f /dev/null &
  121. # wait TERM signal
  122. waitterm
  123. echo "==> STOP"
  124. stop_canal
  125. stop_exporter
  126. echo "==> STOP SUCCESSFUL ..."