Mac

mac 查看端口占用进程

  1. lsof -i tcp:4723

杀掉进程

  1. kill pid

Windows

查找进程号

  1. netstat -ano | findstr 4723

杀掉进程

  1. taskkill -f -pid pid

2-1启动appium服务.mp4 (61.16MB)

code

  1. import subprocess
  2. import os,sys
  3. def stop_appium(port):
  4. mac_cmd = f"lsof -i tcp:{port}"
  5. win_cmd = f"netstat -ano | findstr {port}"
  6. # 判断操作系统
  7. os_platform = sys.platform
  8. if os_platform == "win32": #windows 系统
  9. win_p = subprocess.Popen(win_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  10. for line in win_p.stdout.readlines():
  11. if line:
  12. line = line.decode('utf8')
  13. if "LISTENING" in line:
  14. win_pid = line.split("LISTENING")[1].strip()
  15. os.system(f"taskkill -f -pid {win_pid}")
  16. else: # unix系统
  17. p = subprocess.Popen(mac_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  18. for line in p.stdout.readlines():
  19. line = line.decode('utf8')
  20. # print("line",line)
  21. if "node" in line:
  22. stdoutline = line.split(" ")
  23. print(stdoutline)
  24. pid = stdoutline[4]
  25. os.system(f"kill {pid}")
  26. def start_appium(port):
  27. stop_appium(port)
  28. cmd = f"appium -p {port}"
  29. subprocess.Popen(cmd,shell=True, stdout=open('./logs/appium-logs/'+str(port)+".log",mode='a',encoding="utf8"),
  30. stderr=subprocess.PIPE)
  31. if __name__ == '__main__':
  32. start_appium(4700)
  33. # stop_appium(4700)