Restart & Troubleshoot Jupyter Notebook Service (Linux Command Line)

So this morning I deleted a virtual environment without realizing that it was the one my Jupyter Notebook was using…needless to say, localhost:8888 became nonresponsive afterwards. Here is what you can do if you find yourself in a similar situation, which means you had Jupyter running as a service on your Linux machine.

Troubleshooting your Jupyter Service

First, find your jupyter.service file. It would normally be saved as /etc/systemd/system/jupyter.service

If you open it, you should see something like this

[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/bin/bash -c ". /path/to/your/venv/venv/bin/activate;jupyter-notebook --notebook-dir=/where/your/notebooks/are/located"
User=your-username
Group=your-username
WorkingDirectory=/where/your/notebooks/are/located
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Basically what you want to do is to run the ExecStart command and see whats wrong. 

# Run your version of the following command directly in your command line until you figure our why it doesn't work
# You know you've successfully debbugged it when it results in a notebook that opens in your browser
/bin/bash -c ". /path/to/your/venv/venv/bin/activate;jupyter-notebook --notebook-dir=/where/your/notebooks/are/located"

 

Note: In my case, because I had deleted the old virtual environment, I also had to install Jupyter in my new venv. You can do this by

# activate the new virtual environment
. /path/to/your/venv/venv/bin/activate
# install jupyter
pip install jupyter notebook

 

 

Restarting Jupyter

Once you are satistifed with the new ExecStart, edit your jupyter.service file to update it (you may want to create a backup of the original file first).

Then you just need to replace the service and restart it like any other linux daemon

# copy your new (with working ExecStart command) jupyter.service
sudo cp jupyter.service /etc/systemd/system/

# reload
sudo systemctl daemon-reload

# restart
sudo systemctl stop jupyter.service
sudo systemctl start jupyter.service
sudo systemctl status jupyter.service

At this point, status should be green, and localhost:8888 should work again.