service.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/bin/bash
  2. # 绝对路径
  3. abspath=$(cd "$(dirname "$0")";pwd)
  4. source $abspath/func.sh
  5. function start_process() {
  6. lastProcess=`ps aux | grep skynet | grep $real_path/run/etc/$1.conf | grep -v grep`
  7. if [ -z "$lastProcess" ]; then
  8. if [[ "$2" == "macosx" ]] ; then
  9. yellow_echo "nohup ../skynet/skynet ./run/etc/$1.conf &> /dev/null &"
  10. nohup ../skynet/skynet ./run/etc/$1.conf &> /dev/null &
  11. else
  12. echo "$real_path/skynet/skynet $real_path/run/etc/$1.conf"
  13. $real_path/skynet/skynet $real_path/run/etc/$1.conf
  14. fi
  15. else
  16. red_echo "进程$1已经存在,禁止重复启动"&& return
  17. fi
  18. }
  19. function start() {
  20. #获取配置
  21. platform=`get_platform`
  22. nodeList=`get_process_list`
  23. if [[ ! -z $1 && "$1" != "all" ]] ; then
  24. start_process $1 $platform
  25. else
  26. ## 如果$1为空,或者为all,都启动所有
  27. for pname in ${nodeList[@]} ; do
  28. start_process $pname $platform
  29. sleep 1
  30. done
  31. fi
  32. }
  33. # 杀进程
  34. function kill_process() {
  35. check_debug
  36. yellow_echo "killing $1 ..."
  37. res=`ps aux | grep skynet | grep $real_path/run/etc/$1.conf | grep -v tail | grep -v grep | awk '{print $2}'`
  38. [ "$res" != "" ] && kill -9 $res
  39. }
  40. # 强制关服 杀进程
  41. function fstop() {
  42. nodeList=`get_process_list`
  43. if [[ ! -z $1 && "$1" != "all" ]] ; then
  44. kill_process $1
  45. else
  46. ## 如果$1为空,或者为all,杀死本机所有进程
  47. len=0
  48. for pname in ${nodeList[@]} ; do
  49. list[$len]=$pname
  50. let len=$len+1
  51. done
  52. let len=$len-1
  53. for ((i=len;i>=0;i--)) ; do
  54. kill_process ${list[i]}
  55. sleep 0.5
  56. done
  57. fi
  58. }
  59. # 停止进程
  60. function stop_process() {
  61. yellow_echo "send shut down command to $1 server ...."
  62. # dport=`get_config $1_debugport`
  63. # echo "start helpSrv exit"|nc 127.0.0.1 $dport
  64. ps -ef|grep $1 |grep -v grep |awk '{print $2}' | xargs kill -9
  65. }
  66. function stop() {
  67. nodeList=`get_process_list`
  68. if [[ ! -z $1 && "$1" != "all" ]] ; then
  69. if [[ $nodeList =~ $1 ]] ; then
  70. stop_process $1
  71. else
  72. red_echo "本机没有配置启动 $1 进程,无法停止"
  73. fi
  74. else
  75. ## 如果$1为空,或者为all,停止本机所有进程
  76. len=0
  77. for pname in ${nodeList[@]} ; do
  78. list[$len]=$pname
  79. let len=$len+1
  80. done
  81. let len=$len-1
  82. for ((i=len;i>=0;i--)) ; do
  83. stop_process ${list[i]}
  84. sleep 1
  85. done
  86. fi
  87. ps -ef|grep skynet
  88. }
  89. # 脚本默认参数,check: 打印出正在运行的进程,进程名有颜色显示s
  90. function check() {
  91. nodeList=`get_process_list`
  92. for pname in ${nodeList[@]} ; do
  93. filter="$real_path/skynet/skynet $real_path/run/etc/$pname.conf|${filter}"
  94. done
  95. ps axo pid,%cpu,%mem,rss,vsize,time,command | head -1
  96. ps axo pid,%cpu,%mem,rss,vsize,time,command | egrep "${filter%?}" | grep -v grep | egrep --color=auto "${nodeList[@]//\ /|}"
  97. }
  98. function update_code() {
  99. # 更新逻辑代码
  100. if [[ "$1" == "all" || "$1" == "logic" ]] ; then
  101. cd $real_path/
  102. git pull
  103. fi
  104. # 更新配置
  105. if [[ "$1" == "all" || "$1" == "config" ]] ; then
  106. cd $real_path/config/
  107. git pull
  108. fi
  109. # 更新协议
  110. if [[ "$1" == "all" || "$1" == "proto" ]] ; then
  111. cd $real_path/proto/
  112. git pull
  113. fi
  114. }
  115. # 更新进程
  116. function update_process() {
  117. if [[ ! -n $2 ]]; then
  118. green_echo "请指明更新 {config|logic|proto|}" && exit
  119. fi
  120. yellow_echo "send update [$2] command to $1 server ...."
  121. dport=`get_config $1_debugport`
  122. #首字母大写
  123. par=`echo $2|awk '{print toupper(substr($0,0,1))substr($0,2,length($0))}'`
  124. echo "start helpSrv update$par"|nc 127.0.0.1 $dport
  125. }
  126. # 更新
  127. function update() {
  128. update_succ=`update_code`
  129. nodeList=`get_process_list`
  130. if [[ ! -z $1 && "$1" != "all" ]] ; then
  131. if [[ $nodeList =~ $1 ]] ; then
  132. update_process $1 $2
  133. else
  134. red_echo "本机没有配置启动 $1 进程, 无法更新 $2 "
  135. fi
  136. else
  137. ## 如果$1为空,或者为all,更新本机所有进程
  138. len=0
  139. for pname in ${nodeList[@]} ; do
  140. list[$len]=$pname
  141. let len=$len+1
  142. done
  143. let len=$len-1
  144. for ((i=len;i>=0;i--)) ; do
  145. update_process ${list[i]} $2
  146. done
  147. fi
  148. }
  149. # Main方法,脚本入口,目前该脚本支持start|stop|check|kill|update 参数
  150. case $1 in
  151. check|"")
  152. set_global
  153. check
  154. ;;
  155. start)
  156. set_global
  157. mk_log_dir
  158. start $2
  159. ;;
  160. restart)
  161. set_global
  162. mk_log_dir
  163. check_debug
  164. fstop $2
  165. sleep 1
  166. start $2
  167. ;;
  168. stop)
  169. set_global
  170. stop $2
  171. ;;
  172. kill)
  173. set_global
  174. fstop $2
  175. ;;
  176. update)
  177. set_global
  178. update $2 $3
  179. ;;
  180. git)
  181. set_global
  182. update_code $2
  183. ;;
  184. *)
  185. red_echo "Usage: sh $0 {start|stop|kill|update|check(default)}"
  186. esac