Running a Long-Running Jupyter Notebook Remotely using tmux
-
Start
tmux
Session- Connect to your remote server via SSH:
ssh user@remote-server
- Start a
tmux
session to keep your terminal session active even after disconnection:tmux
- Optionally, you can name the session for easier access later:
tmux new -s my_jupyter_session
- Connect to your remote server via SSH:
-
Launch Jupyter Notebook
- In the
tmux
session, navigate to the folder containing your Jupyter notebooks and start the Jupyter server:jupyter notebook --no-browser --port=8888
- Note down the URL with the token that appears in the terminal output. You will need this to access the notebook later.
- In the
-
Detach from the
tmux
Session- To leave the
tmux
session without stopping it, press:Ctrl+b, then d
- This will detach the
tmux
session, keeping your Jupyter server running.
- To leave the
-
Access the Jupyter Notebook from Your Local Machine
- Create an SSH tunnel to forward the Jupyter port to your local machine:
ssh -L 8888:localhost:8888 user@remote-server
- Open your browser and go to:
http://localhost:8888
- Use the token noted earlier to access your running Jupyter notebook.
- Create an SSH tunnel to forward the Jupyter port to your local machine:
-
Reattach to
tmux
Session (Optional)- If you need to reattach to your
tmux
session to check on Jupyter:tmux attach -t my_jupyter_session
- If you need to reattach to your
If you are using VSCode's Jupyter extension and want to keep the kernel running even after disconnecting from the server, follow these steps:
-
Start a Jupyter Server in
tmux
- SSH into your remote server and start a
tmux
session:ssh user@remote-server tmux new -s my_jupyter_session
- Start the Jupyter server in the
tmux
session:jupyter notebook --no-browser --port=8888
- Note down the URL and token displayed.
- SSH into your remote server and start a
-
Configure VSCode to Use the Running Jupyter Server
- On your local machine, create an SSH tunnel to forward the Jupyter port:
ssh -L 8888:localhost:8888 user@remote-server
- Open VSCode and press
Ctrl+Shift+P
(orCmd+Shift+P
on macOS) to open the Command Palette. - Select "Jupyter: Specify Jupyter Server for Connections" and enter the URL of your running Jupyter server, such as:
http://localhost:8888
- Use the token you noted earlier to connect.
- On your local machine, create an SSH tunnel to forward the Jupyter port:
-
Detach from
tmux
- Detach from the
tmux
session to keep the Jupyter server running in the background:Ctrl+b, then d
- Detach from the
To check if your Jupyter server is still running without reattaching to tmux
, you can use the following commands:
-
List Running Jupyter Notebook Servers
jupyter notebook list
This command will display all currently running Jupyter servers, including their URLs and tokens, if they are still active.
Example output:
Currently running servers: http://localhost:8888/?token=TOKEN_HERE :: /path/to/notebooks
-
List Running Jupyter Processes
- Use the following command to check all Jupyter processes by your user:
ps aux | grep jupyter | grep $(whoami)
- This will list all Jupyter-related processes for the current user
$(whoami)
. Look for entries containingjupyter-notebook
orkernel
to confirm they are still running.
Example output:
melashri 12345 0.5 1.0 456789 123456 ? Sl 14:30 0:20 /usr/bin/python3 /path/to/jupyter/runtime/kernel-XXXX.json
- Use the following command to check all Jupyter processes by your user:
- Use
tmux
to start and keep a Jupyter server running on a remote server. - Detach from the
tmux
session to leave it running in the background. - Use SSH tunneling to access the Jupyter notebook from your local machine.
- Configure VSCode to connect to the running Jupyter server if using the VSCode Jupyter extension.
- To check if the Jupyter server is still running, use
jupyter notebook list
orps aux | grep jupyter
.