123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #!/bin/bash
- # 绝对路径
- abspath=$(cd "$(dirname "$0")";pwd)
- source $abspath/func.sh
- function start_process() {
- lastProcess=`ps aux | grep skynet | grep $real_path/run/etc/$1.conf | grep -v grep`
- if [ -z "$lastProcess" ]; then
- if [[ "$2" == "macosx" ]] ; then
- yellow_echo "nohup ../skynet/skynet ./run/etc/$1.conf &> /dev/null &"
- nohup ../skynet/skynet ./run/etc/$1.conf &> /dev/null &
- else
- echo "$real_path/skynet/skynet $real_path/run/etc/$1.conf"
- $real_path/skynet/skynet $real_path/run/etc/$1.conf
- fi
- else
- red_echo "进程$1已经存在,禁止重复启动"&& return
- fi
- }
- function start() {
- #获取配置
- platform=`get_platform`
- nodeList=`get_process_list`
- if [[ ! -z $1 && "$1" != "all" ]] ; then
- start_process $1 $platform
- else
- ## 如果$1为空,或者为all,都启动所有
- for pname in ${nodeList[@]} ; do
- start_process $pname $platform
- sleep 1
- done
- fi
- }
- # 杀进程
- function kill_process() {
- check_debug
- yellow_echo "killing $1 ..."
- res=`ps aux | grep skynet | grep $real_path/run/etc/$1.conf | grep -v tail | grep -v grep | awk '{print $2}'`
- [ "$res" != "" ] && kill -9 $res
- }
- # 强制关服 杀进程
- function fstop() {
- nodeList=`get_process_list`
- if [[ ! -z $1 && "$1" != "all" ]] ; then
- kill_process $1
- else
- ## 如果$1为空,或者为all,杀死本机所有进程
- len=0
- for pname in ${nodeList[@]} ; do
- list[$len]=$pname
- let len=$len+1
- done
- let len=$len-1
- for ((i=len;i>=0;i--)) ; do
- kill_process ${list[i]}
- sleep 0.5
- done
- fi
- }
- # 停止进程
- function stop_process() {
- yellow_echo "send shut down command to $1 server ...."
- # dport=`get_config $1_debugport`
- # echo "start helpSrv exit"|nc 127.0.0.1 $dport
- ps -ef|grep $1 |grep -v grep |awk '{print $2}' | xargs kill -9
- }
- function stop() {
- nodeList=`get_process_list`
- if [[ ! -z $1 && "$1" != "all" ]] ; then
- if [[ $nodeList =~ $1 ]] ; then
- stop_process $1
- else
- red_echo "本机没有配置启动 $1 进程,无法停止"
- fi
- else
- ## 如果$1为空,或者为all,停止本机所有进程
- len=0
- for pname in ${nodeList[@]} ; do
- list[$len]=$pname
- let len=$len+1
- done
- let len=$len-1
- for ((i=len;i>=0;i--)) ; do
- stop_process ${list[i]}
- sleep 1
- done
- fi
- ps -ef|grep skynet
- }
- # 脚本默认参数,check: 打印出正在运行的进程,进程名有颜色显示s
- function check() {
- nodeList=`get_process_list`
- for pname in ${nodeList[@]} ; do
- filter="$real_path/skynet/skynet $real_path/run/etc/$pname.conf|${filter}"
- done
- ps axo pid,%cpu,%mem,rss,vsize,time,command | head -1
- ps axo pid,%cpu,%mem,rss,vsize,time,command | egrep "${filter%?}" | grep -v grep | egrep --color=auto "${nodeList[@]//\ /|}"
- }
- function update_code() {
- # 更新逻辑代码
- if [[ "$1" == "all" || "$1" == "logic" ]] ; then
- cd $real_path/
- git pull
- fi
- # 更新配置
- if [[ "$1" == "all" || "$1" == "config" ]] ; then
- cd $real_path/config/
- git pull
- fi
- # 更新协议
- if [[ "$1" == "all" || "$1" == "proto" ]] ; then
- cd $real_path/proto/
- git pull
- fi
- }
- # 更新进程
- function update_process() {
- if [[ ! -n $2 ]]; then
- green_echo "请指明更新 {config|logic|proto|}" && exit
- fi
- yellow_echo "send update [$2] command to $1 server ...."
- dport=`get_config $1_debugport`
- #首字母大写
- par=`echo $2|awk '{print toupper(substr($0,0,1))substr($0,2,length($0))}'`
- echo "start helpSrv update$par"|nc 127.0.0.1 $dport
- }
- # 更新
- function update() {
- update_succ=`update_code`
- nodeList=`get_process_list`
- if [[ ! -z $1 && "$1" != "all" ]] ; then
- if [[ $nodeList =~ $1 ]] ; then
- update_process $1 $2
- else
- red_echo "本机没有配置启动 $1 进程, 无法更新 $2 "
- fi
- else
- ## 如果$1为空,或者为all,更新本机所有进程
- len=0
- for pname in ${nodeList[@]} ; do
- list[$len]=$pname
- let len=$len+1
- done
- let len=$len-1
- for ((i=len;i>=0;i--)) ; do
- update_process ${list[i]} $2
- done
- fi
- }
- # Main方法,脚本入口,目前该脚本支持start|stop|check|kill|update 参数
- case $1 in
- check|"")
- set_global
- check
- ;;
- start)
- set_global
- mk_log_dir
- start $2
- ;;
- restart)
- set_global
- mk_log_dir
- check_debug
- fstop $2
- sleep 1
- start $2
- ;;
- stop)
- set_global
- stop $2
- ;;
- kill)
- set_global
- fstop $2
- ;;
- update)
- set_global
- update $2 $3
- ;;
- git)
- set_global
- update_code $2
- ;;
- *)
- red_echo "Usage: sh $0 {start|stop|kill|update|check(default)}"
- esac
|