Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Created October 9, 2024 16:40
Show Gist options
  • Save MohamedElashri/eb8093bb8ae723ae0b739857a80b5716 to your computer and use it in GitHub Desktop.
Save MohamedElashri/eb8093bb8ae723ae0b739857a80b5716 to your computer and use it in GitHub Desktop.
Running a Long-Running Jupyter Notebook Remotely using tmux

Running a Long-Running Jupyter Notebook Remotely using tmux

Step-by-Step Instructions:

  1. 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
  2. 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.
  3. 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.
  4. 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.
  5. Reattach to tmux Session (Optional)

    • If you need to reattach to your tmux session to check on Jupyter:
      tmux attach -t my_jupyter_session

Running a Long-Running Jupyter Notebook using VSCode Jupyter Extension

If you are using VSCode's Jupyter extension and want to keep the kernel running even after disconnecting from the server, follow these steps:

  1. 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.
  2. 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 (or Cmd+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.
  3. Detach from tmux

    • Detach from the tmux session to keep the Jupyter server running in the background:
      Ctrl+b, then d
      

Checking if Jupyter is Running Without Reconnecting

To check if your Jupyter server is still running without reattaching to tmux, you can use the following commands:

  1. 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
    
  2. 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 containing jupyter-notebook or kernel 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
    

Summary of what to do

  • 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 or ps aux | grep jupyter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment