diff --git a/server/bin/mange.py b/server/bin/mange.py new file mode 100755 index 0000000..7715216 --- /dev/null +++ b/server/bin/mange.py @@ -0,0 +1,75 @@ +#!/usr/bin/python +#coding utf8 + +import os +import sys +import time + +def getRuningProgramPids(progname): + pids = [] + lines = os.popen('ps -ef | grep %s' % progname).readlines() + for l in lines: + line = '' + oldc = '' + for c in l.strip(): + if c in [' ', '\t'] and c == oldc: + continue + oldc = c + line += c + line = line.split(' ') + + if line[7] == './%s' % progname: + pids.append(line[1]) + return pids + +def getExePath(pid): + return os.popen('ls -l /proc/%d | grep "exe ->" | cut -d " " -f 7-' % int(pid)).read() + +def getExeCmdLine(pid): + return os.popen('cat /proc/%d/cmdline' % int(pid)).read() + +def stop(instance_id, node_id): + gameserver_ids = getRuningProgramPids('gameserver') + pids = gameserver_ids + for pid in pids: + exepath = getExePath(pid) + cmdline = getExeCmdLine(pid) + if cmdline == ("./gameserver\0-i\0%d\0-i\0%d\0" % (instance_id, node_id)): + os.popen('kill -9 %d' % int(pid)) + +def listServer(): + gameserver_ids = getRuningProgramPids('gameserver') + pids = gameserver_ids + for pid in pids: + exepath = getExePath(pid) + cmdline = getExeCmdLine(pid) + print(pid, exepath, cmdline) + +def restartServer(str_instance_ids, str_node_id): + instance_ids = str_instance_ids.split(',') + node_id = int(str_node_id) + for instance_id in instance_ids: + instance_id = int(instance_id) + stop(instance_id, node_id) + time.sleep(0.5) + print('gameserver %d starting......' % instance_id) + cmd = 'sh start_instance.sh %d %d' % (instance_id, node_id) + os.popen(cmd) + time.sleep(0.5) + +def printHelp(): + print('usuage: [restart]') + +def main(argv): + if len(argv) == 1: + printHelp() + else: + if argv[1] == 'restart': + restartServer(argv[2], argv[3]) + elif argv[1] == 'stop': + stop(argv[2], argv[3]) + elif argv[1] == 'list': + listServer() + +if __name__ == '__main__': + main(sys.argv) diff --git a/server/bin/restart.sh b/server/bin/restart.sh index 06bac8c..7d3ed21 100755 --- a/server/bin/restart.sh +++ b/server/bin/restart.sh @@ -1,31 +1,5 @@ #!/bin/bash -source /etc/profile -source /root/.bash_profile +python manage.py restart $1 $2 -function kill_old_servers() { - ps -ef|grep "valgrind ./gameserver -i$1"|grep -v grep - if [ $? -eq 0 ]; then - ps -ef|grep "valgrind ./gameserver -i$1"|grep -v grep|awk '{print $2}'|xargs kill -9 - fi - - ps -ef|grep "gameserver -i$1"|grep -v grep - if [ $? -eq 0 ]; then - ps -ef|grep "gameserver -i$1"|grep -v grep|awk '{print $2}'|xargs kill -9 - fi -} - -function start_new_servers() { - nouse_valgrind=$2 - if [ "$nouse_valgrind" = "1" ]; then - nohup ./gameserver -i $1 > gameserver.out 2>&1 & - sleep 1 - else - nohup valgrind ./gameserver -i $1 > gameserver.out 2>&1 & - sleep 1 - fi - echo restart ok -} - -kill_old_servers -start_new_servers $1 $2 +echo 'success'