8

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.

1
  • I tried on Ubuntu 20.04 and it was resolved when I activated the respective conda environment. Feb 21 at 18:04

3 Answers 3

10

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.

5
  • @CarlosSouza Can you post a few more details as a separate question? Jan 11, 2020 at 17:05
  • 1
    Sure: I did exact as you described: - created a /etc/rc.local - included the line as you showed on an Ubuntu 18.04, EC2 instance. Nothing happens. Jan 12, 2020 at 8:30
  • @CarlosSouza: Best to open a new question or discuss it in chat: chat.stackoverflow.com/rooms/205822/… Jan 12, 2020 at 8:42
  • @CarlosSouza: Can you try to execute your /etc/rc.local file via and check if there are any error messages? Jan 12, 2020 at 20:15
  • @CésarArquero: Just got a fresh install of Ubuntu 20.04 and updated the answer. Jun 1, 2020 at 19:22
2

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
1
+50

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.

2
  • In my case, it took a little bit of time to start up script to start jupyter, but this solution really works really well. I guess the lag time depends on the computer. This solution easy to execute and importantly does not involve the use of sudo privileges. So less likely to mess up the system upon a silly mistake. For making the script executable chmod +x jupyterlab.sh will also do.
    – user3521099
    Nov 5, 2021 at 13:20
  • I had a few suggestions to improve the answer but could not add them in, because the "edit queue is full". Could you please add them from this link: pastebin.com/QcPfVcKG
    – user3521099
    Nov 5, 2021 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.