用命令实现CTRL_C 退出程序
利用命令或脚本实现按CTRL_C退出的程序
Linux
用linux自带的命令实现
kill -2 pid
windows
用python os.kill实现
import os,signal,re,sys
from subprocess import *
#tasklist | findstr socwatch
#socwatch.exe 7880 Console 1 81,804k
killserver = sys.argv[1]
print("kill server:{}".format(killserver))
cmd = "tasklist | findstr {}".format(killserver)
ret = Popen(cmd,stderr=PIPE,stdin=PIPE,stdout=PIPE,shell=True)
o,e = ret.communicate()
print(o,e)
pid = re.findall("{}.*?(\d+)".format(killserver),o.decode())
print("kill pid via CTRL-C",pid)
os.kill(eval(pid[0]),signal.CTRL_C_EVENT)
浙公网安备 33010602011771号