read and kill process in python

  • read the process in your linux system
import os
tmp = os.popen("ps -af").read()
#读取系统上所有的进程
gzclient_count = tmp.count('gzclient')
#常看是否有cmd为'gzclient'的进程,如果有,则值为1,否则为0
# The -a option tells ps to list the processes of all users on the system rather than just those of the current user.
#The -f option generates a listing that contains fewer items of information for each process than the -l option. 
  • kill the process
if gzclient_count > 0:
#如果有正在运行的gaclient进程
    os.system("killall -9 gzclient")
    #结束所有的gzclient进程
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐