如何在Ubuntu LTS 18.04开机自启动Jupyter Notebook
Hey there! Let’s sort out how to get Jupyter Notebook running automatically on boot—no manual terminal required. It sounds like your earlier shell script attempts didn’t pan out, probably because Jupyter was trying to run in the foreground (needing a terminal) or missing critical environment variables that are only available in your user’s terminal session. Let’s walk through reliable, system-specific solutions that’ll fix this:
Linux: Use a systemd Service (Most Stable Option)
Systemd is Linux’s built-in process manager, and it’s perfect for ensuring Jupyter runs in the background on boot without relying on a terminal.
Step 1: Create a systemd service file
Grab sudo privileges and open a new service file with your favorite editor (e.g.,sudo nano /etc/systemd/system/jupyter.service). Paste this configuration, replacing the placeholders with your details:[Unit] Description=Jupyter Notebook Auto-Start Server After=network.target # Wait for network to load before starting [Service] User=your_username # Replace with your actual Linux username ExecStart=/usr/bin/jupyter-notebook --no-browser --port=8888 # Use `which jupyter-notebook` to get the correct path WorkingDirectory=/home/your_username/your_notebook_folder # Replace with your preferred working directory Restart=always # Restart Jupyter if it crashes [Install] WantedBy=multi-user.targetStep 2: Enable and start the service
Run these commands to activate the service:sudo systemctl daemon-reload # Tell systemd about the new service sudo systemctl enable jupyter.service # Set to start on boot sudo systemctl start jupyter.service # Start it right now to testYou can check if it’s running with
systemctl status jupyter.service.
macOS: Use LaunchAgent
Mac uses launchd to manage auto-start processes. We’ll set up a user-level agent that runs when you log in:
Step 1: Create a plist configuration file
Open a text editor and save this as~/Library/LaunchAgents/com.jupyter.notebook.plist(replace placeholders):<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.jupyter.notebook</string> <key>ProgramArguments</key> <array> <string>/usr/local/bin/jupyter-notebook</string> # Use `which jupyter-notebook` to confirm the path <string>--no-browser</string> <string>--port=8888</string> </array> <key>WorkingDirectory</key> <string>/Users/your_username/your_notebook_folder</string> # Your notebook directory <key>RunAtLoad</key> <true/> # Run when you log in <key>KeepAlive</key> <true/> # Restart if it stops unexpectedly </dict> </plist>Step 2: Load the agent
Run this in terminal to activate it:launchctl load ~/Library/LaunchAgents/com.jupyter.notebook.plistRestart your Mac, and Jupyter will run in the background automatically.
Windows: Task Scheduler
Windows users can leverage Task Scheduler to launch Jupyter on boot or logon:
- Step 1: Open Task Scheduler and create a basic task
- Search for "Task Scheduler" in the Start Menu, then click Create Basic Task.
- Name it "Jupyter Notebook Auto-Start" and add a description if you want.
- Set the trigger to At log on (or At startup if you want it to run before logging in, though that requires admin rights).
- For the action, select Start a program, then browse to your
jupyter-notebook.exepath (typically inC:\Users\your_username\AppData\Local\Programs\Python\PythonXX\Scripts\jupyter-notebook.exewhereXXis your Python version). - Add arguments:
--no-browser --port=8888to run it without opening a browser window. - Finish, and check the box for Run whether user is logged on or not (if you choose "At startup").
Why Your Shell Script Failed
The most common issues are:
- Your script ran Jupyter in the foreground, which requires a terminal to stay active. Adding
--no-browserand appending&to the command would run it in the background, but this isn’t reliable for boot (the process might die if the session ends). - Missing environment variables: When running scripts on boot, the system doesn’t load your user’s full terminal environment (like
PATHto Python/Jupyter). Using systemd/launchd/Task Scheduler ensures the process uses the correct user environment.
内容的提问来源于stack exchange,提问作者Radwa Khattab

