Joalito
Joalito

Reputation: 665

cannot quit jupyter notebook server running

I am using Jupyter Notebook for a project. Since I ssh into a linux cluster at work I use

ssh -Y -L 8000:localhost:8888 user@host

Then I start the notebook with jupyter notebook --no-browser & so that I can continue using the terminal. Then on my local machine I open to localhost:8000 and go about my work.

My problem is that I forgot several times to close the server by foregrounding the process and killing it with Ctrl-C. Instead I just logged out of the ssh session. Now when I run jupyter notebook list I get

Currently running servers:
http://localhost:8934/ :: /export/home/jbalsells
http://localhost:8870/ :: /export/home/jbalsells
http://localhost:8892/ :: /export/home/jbalsells
http://localhost:8891/ :: /export/home/jbalsells
http://localhost:8890/ :: /export/home/jbalsells
http://localhost:8889/ :: /export/home/jbalsells
http://localhost:8888/ :: /export/home/jbalsells

I obviously do not want all of these servers running on my work's machine, but I do not know how to close them!

When I run ps I get nothing:

  PID TTY          TIME CMD
12678 pts/13   00:00:00 bash
22584 pts/13   00:00:00 ps

I have Jupyter 4.1.0 installed.

Upvotes: 27

Views: 65352

Answers (10)

Bohdan Pylypenko
Bohdan Pylypenko

Reputation: 1234

Now, there is a native jupyter command to stop the jupyter server:

for Jupyter Notebook:

jupyter notebook stop [port]

for JupyterLab:

jupyter server stop [port]

Upvotes: 0

amc
amc

Reputation: 833

What worked for me was

  1. jupyter notebook list, which in my case returned:
http://localhost:8889/?token=77d01d687da830b74eba946060660d :: /gpfs/blah/
http://localhost:8889/?token=1243162854ee3648e3154b26643794 :: /ifs/hello/world/
  1. netstat -tulpn | grep "8888", which in my case returned:
tcp        7      0 127.0.0.1:8888          0.0.0.0:*               LISTEN      17602/python3.9

And I found the PID in the last column: 17602.

  1. kill -9 17602, which freed up the port.

Upvotes: 0

ako
ako

Reputation: 11

Here's a bash script that will kill ALL active Jupyter notebook servers at one go, based on the answers given by @Joalito and @Hongsoog:

#!/bin/bash
jupyter notebook list | {
  while IFS= read -r line
  do
    port=`echo "$line" | grep -o -P '(?<=localhost:).*(?=/ :)'`
    echo "killing jn in port $port"
    if [ -z "$port" ]
    then
      netstat -tulpn | grep "$port" | grep -o -P '(?<=LISTEN      ).*(?=/py)' | xargs kill -15
    fi
  done
}

Upvotes: 1

tariksalay
tariksalay

Reputation: 387

On your notebook the welcoming page is named "Files" and you can see "Running" next to it. There is where you would want to shutdown and see the running notebooks

Upvotes: 0

smsarwar
smsarwar

Reputation: 371

Use the following command to stop Jupyter notebook running on port 8888:

fuser -k 8888/tcp 

Upvotes: 14

ShusenTang
ShusenTang

Reputation: 221

This might help:

  1. run jupyter notebook list to get the port number jupyter uses.
  2. run lsof -n -i4TCP:[port-number] to get PID. The PID is the second field in the output.
  3. run kill -9 [PID] to kill this process.

Upvotes: 13

Hongsoog
Hongsoog

Reputation: 553

Windows Systems commands on Command Prompt

Be careful to save all the changes made in your notebooks prior to kill the jupyter notebook server process.

i) find the port number used by jupyter notebook server

jupyter notebook list

ex.)

jupyter notebook list
Currently running servers:
http://127.0.0.1:8888/ :: D:\kimkk\Documents\JupyterNotebook

ii) find process ids that use the found port number of jupyter notebook

netstat -ano | find "found portnumber"

ex.)

netstat -ano | find "8888"

  TCP    127.0.0.1:8888         0.0.0.0:0              LISTENING       24140
  TCP    127.0.0.1:8888         127.0.0.1:55588        ESTABLISHED     24140
  TCP    127.0.0.1:8888         127.0.0.1:55612        ESTABLISHED     24140
  TCP    127.0.0.1:55588        127.0.0.1:8888         ESTABLISHED     6492
  TCP    127.0.0.1:55612        127.0.0.1:8888         ESTABLISHED     6492
  • find rows with second column value equals to "8888". In above example first, second, and third rows are target rows. In those rows, you can find PID in the last column (ex. 24140).

iii) kill jupyter notebook process with found PID

taskkill /PID found_PID /F

ex.)

taskkill /PID 24140 /F
  • /F means forcely kill the process.

FYI, Jupyter notebook from version 5.1 supports stop command as follows:

jupyter notebook stop 8888

refer to https://github.com/jupyter/notebook/issues/1950

Upvotes: 17

pramithus
pramithus

Reputation: 61

I ran into the same issue and followed the solution posted above. Just wanted to clearify the solution a little bit.

netstat -tulpn

will list all the active connections.

tcp    0   0 0.0.0.0:8888  0.0.0.0:*  LISTEN      19524/python

you will need the PID "19524" in this case. you can even use the following to get the PID of the port you are trying to shut down

fuser 8888/tcp

this will give you 19524 as well.

kill 19524

will shut down the port

Upvotes: 6

Joalito
Joalito

Reputation: 665

So I found a solution.

Since jupyter notebook list tells you which ports the notebook servers are running on I looked for the PIDs using netstat -tulpn I got the information from http://www.cyberciti.biz/faq/what-process-has-open-linux-port/

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
PID/Program name    
tcp        0      0 0.0.0.0:8649            0.0.0.0:*               LISTEN      
-                   
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      
-                   
tcp        0      0 0.0.0.0:33483           0.0.0.0:*               LISTEN      
-                   
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN      
39125/Xvnc          

Without looking too hard I was able to find the ports I knew to look for from jupyter notebook list and the processes running them (you could use grep if it were too hard to find them). Then I killed them with kill 8337 (or whatever number was associated).

Upvotes: 27

Ceddrick
Ceddrick

Reputation: 158

Section 3.3 should be applicable to this. http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html

When a notebook is opened, its “computational engine” (called the kernel) is automatically started. Closing the notebook browser tab, will not shut down the kernel, instead the kernel will keep running until is explicitly shut down.

To shut down a kernel, go to the associated notebook and click on menu File -> Close and Halt. Alternatively, the Notebook Dashboard has a tab named Running that shows all the running notebooks (i.e. kernels) and allows shutting them down (by clicking on a Shutdown button).

Upvotes: 1

Related Questions