Mac
mac 查看端口占用进程
lsof -i tcp:4723
杀掉进程
kill pid
Windows
查找进程号
netstat -ano | findstr 4723
杀掉进程
taskkill -f -pid pid
code
import subprocess
import os,sys
def stop_appium(port):
mac_cmd = f"lsof -i tcp:{port}"
win_cmd = f"netstat -ano | findstr {port}"
# 判断操作系统
os_platform = sys.platform
if os_platform == "win32": #windows 系统
win_p = subprocess.Popen(win_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
for line in win_p.stdout.readlines():
if line:
line = line.decode('utf8')
if "LISTENING" in line:
win_pid = line.split("LISTENING")[1].strip()
os.system(f"taskkill -f -pid {win_pid}")
else: # unix系统
p = subprocess.Popen(mac_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
for line in p.stdout.readlines():
line = line.decode('utf8')
# print("line",line)
if "node" in line:
stdoutline = line.split(" ")
print(stdoutline)
pid = stdoutline[4]
os.system(f"kill {pid}")
def start_appium(port):
stop_appium(port)
cmd = f"appium -p {port}"
subprocess.Popen(cmd,shell=True, stdout=open('./logs/appium-logs/'+str(port)+".log",mode='a',encoding="utf8"),
stderr=subprocess.PIPE)
if __name__ == '__main__':
start_appium(4700)
# stop_appium(4700)