该脚本实现通过进程名获取到所有进程id,然后计算每个进程的线程数并相加。
from subprocess import check_output,getoutput
def get_pid(name): #获取应用进程id
return map(int,check_output(["pidof",name]).split())
if __name__ == '__main__':
result=0
for pid in get_pid('sshd'): #sshd 替换为对应进程名
output = getoutput('pstree -p %s | wc -l'%(pid)) #返回线程数
result=result+int(output) #计算线程数总和
print (result)