You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google实例上用nohup后台运行Jupyter Notebook

Running Jupyter Notebook Persistently on Google Cloud Instances (Even After Closing Terminal)

Got it, let's get this sorted for you! Using nohup to keep Jupyter running in the background is the right approach—let's walk through the correct steps, fix common issues you might be hitting, and even cover a more flexible alternative if you want it.

First, let's make your Jupyter access smoother so you don't struggle with tokens every time:

  • Generate a config file:
    jupyter notebook --generate-config
    
  • Set a password (way easier than using auto-generated tokens):
    jupyter notebook password
    
    Follow the prompts to create a password you'll use to log in via your browser later.
  • (Optional) Customize the port: Open the config file (usually at ~/.jupyter/jupyter_notebook_config.py) and uncomment/set:
    c.NotebookApp.port = 8888  # You can use any unused port here, like 8889 if 8888 is taken
    

Step 2: Launch Jupyter with nohup Correctly

Here's the exact command you need to run on your Google Cloud instance:

nohup jupyter notebook --no-browser &

Let's break this down:

  • nohup: Tells the system to ignore the "hangup" signal that gets sent when you close your terminal.
  • --no-browser: Critical, since your cloud instance doesn't have a desktop environment to launch a browser locally.
  • &: Puts the Jupyter process in the background so you can keep using your terminal (or close it without killing Jupyter).

After running the command, nohup saves all output (including the Jupyter access URL) to a file called nohup.out in your current directory. To view it:

cat nohup.out

Look for a line like this (if you set a password, you might just see the base URL without the token):

http://localhost:8888/?token=abc123def456...

Step 3: Reconnect via SSH Forwarding (Even After Closing Terminal)

When you close your original terminal, the SSH port forwarding you set up earlier breaks—but Jupyter is still running in the background! To access it again:

  1. Open a new local terminal.
  2. Run the SSH port forwarding command again:
    ssh -L 8888:localhost:8888 your-username@your-instance-external-ip
    
    (Replace 8888 with your custom port if you changed it, and fill in your instance's details.)
  3. Open your local browser and go to http://localhost:8888, then enter the password you set earlier.

Troubleshooting Common Issues

If you're having problems with nohup, here's how to fix the most frequent snags:

  • Can't find the access link: Check nohup.out—that's where all Jupyter logs go. If the file is empty, try running the command again without & first to see if there's an error (like a port conflict).
  • Port is already in use: Find the process using the port and kill it:
    lsof -i :8888  # Replace with your port
    kill -9 <PID>  # Replace <PID> with the process ID from the lsof output
    
    Or just launch Jupyter on a different port:
    nohup jupyter notebook --no-browser --port 8889 &
    
  • Jupyter stops when you close the terminal: Make sure you included both nohup and & in your command. If it still stops, check if your Google Cloud instance is set to shut down on idle (adjust this in the instance settings if needed).

Alternative: Use screen for More Control

If you want a more flexible way to manage background processes (like being able to reattach to the Jupyter terminal later), screen is a great tool:

  1. Install it on your instance (for Debian/Ubuntu):
    sudo apt update && sudo apt install screen
    
  2. Create a new screen session named "jupyter":
    screen -S jupyter
    
  3. Launch Jupyter normally (no need for nohup):
    jupyter notebook --no-browser
    
  4. Detach from the session without closing it: Press Ctrl + A then D.
  5. Later, reattach to the session to check logs or stop Jupyter:
    screen -r jupyter
    

内容的提问来源于stack exchange,提问作者user1700890

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:34:49