control 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/bin/bash
  2. CWD=$(cd $(dirname $0)/; pwd)
  3. cd $CWD
  4. usage()
  5. {
  6. echo $"Usage: $0 {start|stop|restart|status|build|pack} <module>"
  7. exit 0
  8. }
  9. start_all()
  10. {
  11. # http: 1984
  12. test -x urlooker-web && start web
  13. test -x urlooker-alarm && start alarm
  14. test -x urlooker-agent && start agent
  15. }
  16. start()
  17. {
  18. mod=$1
  19. if [ "x${mod}" = "x" ]; then
  20. usage
  21. return
  22. fi
  23. if [ "x${mod}" = "xall" ]; then
  24. start_all
  25. return
  26. fi
  27. binfile=urlooker-${mod}
  28. if [ ! -f $binfile ]; then
  29. echo "file[$binfile] not found"
  30. exit 1
  31. fi
  32. if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -gt 0 ]; then
  33. echo "${mod} already started"
  34. return
  35. fi
  36. mkdir -p logs/$mod
  37. nohup $CWD/$binfile &> logs/${mod}/stdout.log &
  38. for((i=1;i<=15;i++)); do
  39. if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -gt 0 ]; then
  40. echo "${mod} started"
  41. return
  42. fi
  43. sleep 0.2
  44. done
  45. echo "cannot start ${mod}"
  46. exit 1
  47. }
  48. stop_all()
  49. {
  50. test -x urlooker-web && stop web
  51. test -x urlooker-alarm && stop alarm
  52. test -x urlooker-agent && stop agent
  53. }
  54. stop()
  55. {
  56. mod=$1
  57. if [ "x${mod}" = "x" ]; then
  58. usage
  59. return
  60. fi
  61. if [ "x${mod}" = "xall" ]; then
  62. stop_all
  63. return
  64. fi
  65. binfile=urlooker-${mod}
  66. if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -eq 0 ]; then
  67. echo "${mod} already stopped"
  68. return
  69. fi
  70. ps aux|grep -v grep|grep -v control|grep "$binfile"|awk '{print $2}'|xargs kill
  71. for((i=1;i<=15;i++)); do
  72. if [ $(ps aux|grep -v grep|grep -v control|grep "$binfile" -c) -eq 0 ]; then
  73. echo "${mod} stopped"
  74. return
  75. fi
  76. sleep 0.2
  77. done
  78. echo "cannot stop $mod"
  79. exit 1
  80. }
  81. restart()
  82. {
  83. mod=$1
  84. if [ "x${mod}" = "x" ]; then
  85. usage
  86. return
  87. fi
  88. if [ "x${mod}" = "xall" ]; then
  89. stop_all
  90. start_all
  91. return
  92. fi
  93. stop $mod
  94. start $mod
  95. status
  96. }
  97. status()
  98. {
  99. ps aux|grep -v grep|grep "urlooker"
  100. }
  101. build_one()
  102. {
  103. mod=$1
  104. go build -mod=vendor -o urlooker-${mod} modules/${mod}/${mod}.go
  105. }
  106. build()
  107. {
  108. mod=$1
  109. if [ "x${mod}" = "x" ]; then
  110. build_one web
  111. build_one alarm
  112. build_one agent
  113. return
  114. fi
  115. build_one $mod
  116. }
  117. reload()
  118. {
  119. mod=$1
  120. if [ "x${mod}" = "x" ]; then
  121. echo "arg: <mod> is necessary"
  122. return
  123. fi
  124. build_one $mod
  125. restart $mod
  126. }
  127. pack()
  128. {
  129. v=$(date +%Y-%m-%d-%H-%M-%S)
  130. tar zcvf urlooker-$v.tar.gz control \
  131. modules/web/static \
  132. modules/web/views \
  133. sql \
  134. script script \
  135. urlooker-web configs/web.yml \
  136. urlooker-alarm configs/alarm.yml \
  137. urlooker-agent configs/agent.yml
  138. }
  139. case "$1" in
  140. start)
  141. start $2
  142. ;;
  143. stop)
  144. stop $2
  145. ;;
  146. restart)
  147. restart $2
  148. ;;
  149. status)
  150. status
  151. ;;
  152. build)
  153. build $2
  154. ;;
  155. reload)
  156. reload $2
  157. ;;
  158. pack)
  159. pack
  160. ;;
  161. *)
  162. usage
  163. esac