如何真正地关闭jupyter notebook server

很多时候用control+c结束jupyter进程时发现端口还被占用,notebook没有真正关闭,在

jupyter notebook stop

失效的情况下,可以使用

jupyter notebook list #查看当前jupyter端口
lsof -n -i4TCP:[port-number] # 通过端口获得pid
kill -9 [PID] # 删除进程

在jupyter notebook中,你可以用以下的命令关掉所有的jupyter server:

import re

port_lines = !jupyter notebook list
ports = []
for line in port_lines:
    ports+=re.findall(r'(?:http://0.0.0.0:)(\d+)(?:/)',line)
print ports

pids = []
for port in ports:
    out = !lsof -n -i4TCP:$port
    pid = out[1].split()[1]
    pids.append(pid)
print pids

for pid in pids:
    !kill -9 $pid

Ref:
https://github.com/jupyter/notebook/issues/2844#issuecomment-385882596

你可能感兴趣的:(python)