在Linux下面调试程序的时候,经常是使用kill方式来停止程序, 每次要先执行一次 ps 命令,获取到进程的ID,然后使用kill命令停止进程。

程序主要使用os模块的popen创建一个读管道,也就是其结果会读出到 lines 之中。

然后依次遍历,读取到的各个行,同时过滤掉自身 'grep mx_skyfile_serv'。

import os

#lines connect to popen_file's read
lines = os.popen('ps -ef|grep mx_skyfile_serv')
for line in lines:

  if line.find('grep mx_skyfile_serv')!=-1: continue

  vars = line.split()
  pid = vars[1] #get pid
  proc = ''.join(vars[7:]) #get proc description

  out = os.system('kill '+pid)
  if out==0:
    print('success! kill '+pid+' '+proc)
  else:
    print('failed! kill '+pid+' '+proc)

 

相关文章:

  • 2021-12-18
  • 2022-01-25
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-06-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2022-01-05
  • 2021-09-13
  • 2022-12-23
  • 2021-12-12
相关资源
相似解决方案