def get_servpidmapname():
servPidInfoList = []
ostype = get_ostype()
if ostype != "Linux":
f = open(WINDOWS_SERVER_PID_LIST,'r')
count = len(f.readlines())
#print("get count from conf = %s" % (count))
f.close()
i = 0
while i< count:
#读取mode+1行
list = []
i=i+1
if ostype != "Linux":
modestr = linecache.getline(WINDOWS_SERVER_PID_LIST,i+1)
# print("get modestr from conf = %s" % (modestr))
if modestr.strip() == "":
continue
#查找该行key对应的name get str from conf = ['[process]n']
str = modestr.split("=", -1)
servstrName= str[0].strip() #name
servstrPid= str[1] #pid
servstr1 = re.split('n', servstrPid)
# print("get servstrPid from conf name:%s, pid:%s" % (servstrName, servstr1[0]))
proctime = int(time.time())
try:
ptmp = psutil.Process(int(servstr1[0]))
if ptmp.status() == "running":
pidStatus = 1
else:
pidStatus = 2
#print("get serverpid servPidInfoList:%s" % (servPidInfoList))
except Exception as e:
pidStatus = 2
# print("get proc pid status info error")
list.append(pidStatus)
list.append(servstrName)
servPidInfoList.append(list)
print("get servPidInfoList:%s" % (servPidInfoList))
return servPidInfoList
[procinfo] parking_3000 = 80412 video_2010 = 33204 cabin_3020 = 16188
结果:
get servPidInfoList:[[2, 'parking_3000', 1657534235], [2, 'video_2010', 1657534235], [2, 'cabin_3020', 1657534235]]
也可以先将key-value存放如数组列表中然后根据其key获取对应的value
"""
获取pid文件服务名及pid
"""
def get_servpidmapname():
servPidInfoList = []
ostype = get_ostype()
if ostype != "Linux":
f = open(WINDOWS_SERVER_PID_LIST,'r')
count = len(f.readlines())
f.close()
i = 0
try:
while i< count:
#读取mode+1行
itemlist = []
i=i+1
if ostype != "Linux":
modestr = linecache.getline(WINDOWS_SERVER_PID_LIST,i+1)
if modestr.strip() == "":
continue
#查找该行key对应的name get str from conf
str = modestr.split("=", -1)
# print("str:",str)
servstrName = str[0].strip() #name
servstrPid = int(str[1]) #pid
itemlist.append(servstrName)
itemlist.append(servstrPid)
servPidInfoList.append(itemlist)
except:
print("get servpid error.")
# print("get servPidInfoList:%s" % (servPidInfoList))
return servPidInfoList
"""
根据服务名获取对应的pid
"""
def getpidfromname(pidlist,servtype):
servPid = -1
for name,pid in pidlist:
if str(servtype) == str(name):
servPid = pid
break
print("pidName:%s , pidtmp:%d##########" %(servtype,servPid))
return servPid



