View the process id, and kill the process

psutil Import 
Import OS

DEF get_all_pid_name ():
PIDS = psutil.pids ()
for PID in PIDS:
the try:
P = psutil.Process (PID)

Print ( 'PID-% S, S pname-%'% (PID, P. name ()))
IF p.name () == 'chromedriver.exe':
the kill (pid)
the except:
Pass

DEF the kill (pid):
# this function is used to abort the incoming process corresponding to the pid
if os.name = = 'NT':
# the Windows system
cmd = 'the taskkill / PID' + STR (PID) + '/ F'
the try:
the os.system (cmd)
Print (PID, 'killed')
the except Exception AS E:
Print (E)
elif os.name == 'posix':
# Linux系统
cmd = 'kill ' + str(pid)
try:
os.system(cmd)
print(pid, 'killed')
except Exception as e:
print(e)
else:
print('Undefined os.name')

if __name__ == '__main__':
get_all_pid_name()
# kill(65408)

Guess you like

Origin www.cnblogs.com/chenxiyuxiao/p/11007431.html