Run Jupyter-notebook on boot on Ubuntu

Simon Houdayer picture Simon Houdayer · May 28, 2017 · Viewed 11.2k times · Source

I have an Ubuntu 16.04 Virtual Machine with anaconda installed, And I want it to launch Jupyter-notebook on startup with the correct configuration file (ip address, port, password,...)

This configuration is specified in /home/user/.jupyter/jupyter_notebook_config.py

When I'm logged as user and in the home directory(/home/user/) it does launch the correct config file.

But when using the command

jupyter-notebook

During startup with rc.local or using crontab it's doesn't load my configuration file, and have not the correct running directory.

Answer

Maximilian Peters picture Maximilian Peters · Jul 30, 2017

Very similar question and answer: How to start ipython notebook server at boot as daemon

You could add the following line to your /etc/rc.local file

su <username> -c "jupyter notebook --config=/location/of/your/config/file/.jupyter/jupyter_notebook_config.py --no-browser --notebook-dir=/location/of/yournotebooks" &

e.g.

su simon -c "jupyter notebook --config=/home/simon/.jupyter/jupyter_notebook_config.py --no-browser --notebook-dir=/home/simon/notebooks" &

su <username> -c makes sure that the notebook is not executed as root but with the specified user account.``

--config and --notebook-dir specify the location of your config file and your notebook folder (http://jupyter-notebook.readthedocs.io/en/latest/config.html)


For systems using systemd (Ubuntu 16 and later) the following approach also works:

  • Create a service file in /etc/systemd/system/, e.g. jupyter.service with the following content (replace YourUserName with your username)

    [Unit]
    After=network.service
    
    [Service]
    ExecStart=jupyter notebook
    User=YourUserName
    
    [Install]
    WantedBy=default.target
    
  • Enable the service with sudo systemctl enable jupyter.service

  • Start the service with sudo systemctl start jupyter.service

You should set a password for your Jupyter server because you won't have access to the token.