mac 关闭某端口的进程

启动 flask 时报错:OSError: [Errno 48] Address already in use

应该是有谁占用了 5000 端口。解决方法:

  • lsof -i tcp:5000,找到占用此端口的进程号
  • kill {进程号},杀掉进程
  • 再次启动 flask

完整实例:

(venv) ➜  flaskr lsof -i tcp:5000
COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Python  48998 plough    3u  IPv4 0xce1d99ccf653b655      0t0  TCP localhost:commplex-main (LISTEN)
(venv) ➜  flaskr kill 48998
(venv) ➜  flaskr lsof -i tcp:5000
(venv) ➜  flaskr flask run
 * Serving Flask app "flaskr"
 * Forcing debug mode on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)