【Python】Python对程序多次统计脚本

来源:互联网 发布:制作立绘的软件 编辑:程序博客网 时间:2024/06/05 17:14

在参加华为的2015挑战赛中,出现的问题。

问题描述如下:

希望一直循环运行一个脚本,例如10次,每次都需要这个脚本停下来才去执行下一次。脚本会使一个程序gameserver运行,所以每次循环中都需要等待这个进程结束。

每次运行脚本都会生成一个文件,在文件的某个位置能找到自己本次运行的名次,需要统计这个名次。

最终选择使用Python,脚本如下

import osimport stringrun_time = 10 win_time = 0output = open('result.txt','a') for i in range(run_time):print('-----------%d time-------'%i)os.system('/home/game/game/works/target/run.sh')os.system('sleep 10')flag = 1while flag == 1:tmp = os.popen('pidof gameserver').readlines()if len(tmp) == 0:flag = 0else:os.system('sleep 5')tmp = os.popen('tail -3 /home/game/run_area/server/replay.txt | head -1').readlines()result = tmp[-1]char = result[-2]if string.atoi(char) == 1:win_time = win_time + 1print charoutput.write(result)output.write('we run %d times game, and win %d times !!'%(run_time, win_time))output.close()
注意其中Python是如何调用linux的命令,以及如何查找某个进程是否结束。

0 0
原创粉丝点击