Simon
Simon

Reputation: 173

Run Jupyter-notebook on boot on Ubuntu

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.

Upvotes: 8

Views: 20641

Answers (3)

Maximilian Peters
Maximilian Peters

Reputation: 31659

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.

Upvotes: 10

In my case, in ubuntu 20.04 the "/etc/rc.local" way didn't work. I've solved in two steps: (1) creating and executable file that work just with double click or enter; and (2) addind the execution to startup applications in gnome.

Here is in detail:

  1. Create the file in the place you want and add executable option. Here is the one line of code to create the file in the USER folder: cd /home/USER & touch jupyterlab.sh & sudo chmod u+x jupyterlab.sh

  2. Add the corresponding execution excript to the file. In my case I'm running jupyter-lab (locate the program with which jupyter-lab), with an ip and port as I'm using it as server.

Here is what the file contains:

#!/bin/bash
/home/USER/anaconda3/bin/jupyter-lab --ip 192.168.1.32 --port 9000 --no-browser & exit
  1. (optional) Make the file executable also by double click and enter goint to preferences in dolphin (like here).

  2. Add the file and .sh extension to the startup aplications.

enter image description here

May be it seems long, but it has the advantages of getting an executable file that can initialize (or not) just with a few clicks.

Upvotes: 1

Imam AR
Imam AR

Reputation: 106

this one worked for me. Put this in your /etc/rc.local.

sudo -u <username> nohup /home/<username>/.local/bin/jupyter-notebook --ip 0.0.0.0 --port 8888 --no-browser --notebook-dir=/home/<username>/<notebook_dir>&

In my case, /etc/rc.local is not available at the first time, so you need to create this first. Then, make it executable.

sudo chmod +x /etc/rc.local

Here is the content of my rc.local look like.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

sudo -u my_username nohup /home/my_username/.local/bin/jupyter-notebook --ip 0.0.0.0 --port 8888 --no-browser --notebook-dir=/home/my_username/notebook&

exit 0

Upvotes: 2

Related Questions