Register Jupyter Notebook server as a service on Linux

Configure your Jupyter Notebook server to automatically start upon reboot or system crash by registering it as a service on Linux. The following instructions were adapted from this blog post and tested on Ubuntu 18.04 (should work on other versions of Ubuntu or Ubuntu derivatives).

Outline

Install Jupyter

The following command installs Jupyter Notebook on the base (default) conda environment. You can use other python environment.

conda activate base
which pip
pip install notebook==6.0.3
jupyter-notebook --generate-config

Configure Jupyter

Open the config file,

gedit ~/.jupyter/jupyter_notebook_config.py

and find/modify the following lines:

c.NotebookApp.ip = '<your IP address of choice>'
c.NotebookApp.password_required = False
c.NotebookApp.port = <your port number of choice>
c.NotebookApp.token = ''

Adjust the IP address and port number to your liking. You can set the IP address to localhost (127.0.0.1) or internal address (such as 192.168.xxx.xxx) or external address depending on the network configuration.

Create a service

Create a service file as follows:

[Unit]
Description=Jupyter Daemon
After=network.target

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/<USER_NAME>/miniconda3/bin/jupyter-notebook
User=<USER_NAME>
Group=<GROUP_NAME>
WorkingDirectory=/home/<USER_NAME>/<PATH_TO_WORKSPACE>
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Modify the ExecStart, User, Group, WorkingDirectory lines. ExecStart points to the binary that runs the Jupyter Notebook server. User and Group sets the UNIX user/group to run the binary file as. WorkingDirectory sets the default directory for the Jupyter Notebook.

Install the service file with the following commands:

sudo cp jupyter.service /etc/systemd/system/
sudo systemctl enable jupyter.service
sudo systemctl daemon-reload
sudo systemctl start jupyter.service
systemctl status jupyter.service