Run jupyter notebook on system boot

What is the best way to run Jupiter notebook command on system startup so I don’t need to ssh connect? I would like to be able to use the notebooks after aws instance start by just entering the address in a browser. Is it possible?

I do it with crontab.

Edit your crontab with this command:

export EDITOR=nano
crontab -e

And add this line to the bottom

@reboot cd /home/ubuntu; source ~/.bashrc;  /home/ubuntu/anaconda2/bin/jupyter notebook

Save, quit, and reboot. with

sudo reboot

Note: you get charged a full hour every time you start an AWS instance. I don’t think you get charged for just doing a reboot.

6 Likes

oh, thank you for this. I tried several things that did not seem to work. I should have looked on the forums first! Lesson learned

Very nice!

Can I suggest a small improvement?

You might as well make it work regardless of the username by using ‘~’ exclusively to refer to the home directory. Also, it’s handy to output the logs somewhere, in case of problems…

@reboot cd ~/deeplearning; source ~/.bashrc; ~/anaconda3/bin/jupyter notebook >>~/cronrun.log 2>&1

1 Like

Just a reminder for those who may misunderstand the “reboot” thing (like me): doing a reboot will not be charged again, but stopping an instance and restarting it immediately will still result in doubled cost in this hour.

I found systemd works really well for me on my Ubuntu server. There are a bunch of ways to do this, but using systemd on Ubuntu you can set up an “always running” jupyter workplace (e.g. a directory with all your projects). Systemd will handle restarting for you after reboots or crashes.

Create the file: /etc/systemd/system/jupyter.service

[Unit]
Description=Jupyter Workplace

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/bfortuner/anaconda3/bin/jupyter-notebook --config=/home/bfortuner/.jupyter/jupyter_notebook_config.py
User=bfortuner
Group=bfortuner
WorkingDirectory=/home/bfortuner/workplace
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Then, run the commands

systemctl enable jupyter.service
systemctl daemon-reload
systemctl restart jupyter.service
2 Likes

Will this start jupyter notebook after a shutdown/start as well, or only after a reboot? (the @reboot bit makes me wonder)

I find that source ~/.bashrc was not executed by cron. I keep getting the following error when importing utils.py

ERROR (theano.sandbox.cuda): nvcc compiler not found on $PATH. Check your nvcc installation and try again.
Using Theano backend.

I followed @brendan’s systemd way. Here’s the same steps for a paperspace machine:

Create the file: sudo nano /etc/systemd/system/jupyter.service

[Unit]
Description=Jupyter Workplace

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/paperspace/anaconda3/bin/jupyter-notebook --config=/home/paperspace/.jupyter/jupyter_notebook_config.py
User=paperspace
Group=paperspace
WorkingDirectory=/home/paperspace/fastai
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Then, run the commands

sudo systemctl enable jupyter.service
sudo systemctl daemon-reload
sudo systemctl restart jupyter.service

You still need to link machine:8888 to your localhost:8888. So I just added this line to the end of ~/.bashrc:

systemctl status jupyter.service

So I still have to SSH to the machine, but it will display the link with token automatically (using the last steps in the setup).

Thanks for this helpful modification of the systemd setup for use in Paperspace. I had to make one small change to make sure I was running jupyter from the fastai conda env, otherwise some packages were not found on import. So, I simply modified the ExecStart line to read:

ExecStart=/home/paperspace/anaconda3/envs/fastai/bin/jupyter-notebook --config=/home/paperspace/.jupyter/jupyter_notebook_config.py

Seems to be working perfectly now. Thanks so much!
Michael

I need some help.

Now, to start jupyter service I run
jupyter notebook --certfile=~/ssl/mycert.pem --keyfile ~/ssl/mykey.key

I want jupyter to run as a daemon and start in a workdir like ~/ml

This is my configuration so far.

/etc/systemd/system/jupyter.service

   [Unit]
Description=Jupyter Workplace

[Service]
Type=simple
ExecStart=/home/ubuntu/anaconda3/bin/jupyter-notebook
User=ubuntu
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

I don’t know how to add --certfile=~/ssl/mycert.pem --keyfile ~/ssl/mykey.key

When I check the status

    ● jupyter.service - Jupyter Workplace
   Loaded: loaded (/etc/systemd/system/jupyter.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2021-02-18 07:53:01 UTC; 8s ago
 Main PID: 7801 (jupyter-noteboo)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/jupyter.service
           └─7801 /home/ubuntu/anaconda3/bin/python /home/ubuntu/anaconda3/bin/jupyter-notebook

Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [I 07:53:02.493 NotebookApp] JupyterLab extension loaded from /home/ubuntu/anaconda3/lib/python3.7/site-packages/jupyterla
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [I 07:53:02.493 NotebookApp] JupyterLab application directory is /home/ubuntu/anaconda3/share/jupyter/lab
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [I 07:53:02.652 NotebookApp] [nb_conda] enabled
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [I 07:53:02.652 NotebookApp] Serving notebooks from local directory: /
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [I 07:53:02.652 NotebookApp] The Jupyter Notebook is running at:
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [I 07:53:02.652 NotebookApp] http://localhost:8888/
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [I 07:53:02.652 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [W 07:53:02.655 NotebookApp] No web browser found: could not locate runnable browser.
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [I 07:53:02.656 NotebookApp] Starting initial scan of virtual environments...
Feb 18 07:53:02 ip-172-31-41-157 jupyter-notebook[7801]: [E 07:53:02.658 NotebookApp] 'conda' not found in path.