Autostart Jupyter Notebook

I’m not sure if this has been posted before, but I figured I would share something I found useful. I recently blogged about using the Deep Learning AMI, and updated it for the fast.ai AMI as well, and shared some issues I encountered and some solutions.

Since I was always stopping the instance when the I’m not using it, I wanted to find a way to autostart Jupyter when I start up the instance again. Here’s some steps to do that exactly on the fastai AWS AMI:

  1. Create jupyter_start.sh in your home directory
  2. Add the following:
export PATH="$PATH:/home/ubuntu/src/anaconda3/bin"
jupyter notebook --notebook-dir=/home/ubuntu/ --profile=nbserver > /tmp/ipynb.out 2>&1 &
  1. You can change notebook-dir to wherever you want for example /home/ubuntu/fastai/courses/dl1/ if you want to point it directly to the course notebooks
  2. Edit /etc/rc.local and add the following line before exit 0
    su ubuntu -c 'bash /home/ubuntu/jupyter_start.sh'

I’m not sure if this very relevant to people using SSH tunnelling, as I’m mainly accessing using the IP instead, so this works great for my use case. I hope you find this useful!

4 Likes

Is there an easy way to restart jupyter with this solution? I did this for a while, but I had a hard time restarting jupyter because it was in another session so I had to lookup and kill the process in order to restart it.

Yeah, I ran into this problem and found it annoying as well! An easy way to kill it without necessarily looking up the process ID is using pkill jupyter. You can easily put these into bash scripts where for e.g.

jupyter_stop.sh does pkill jupyter, and another jupyter_restart.sh would just simply call the 2 other bash scripts to stop then start!

2 Likes

Very cool, those are all way better than the way I was using.

1 Like

You could use systemctl
/etc/systemd/system/jupyter-gpu.service
[Unit]
Description=Service for jupyter cpu notebook
After=local-fs.target network.target

[Service]
Type=simple
User=chc
Group=chc
ExecStart=/home/chc/start-tf-jupyter-gpu.sh
Restart=always
SyslogIdentifier=jupyter cpu notebook

[Install]
WantedBy=multi-user.target

To run the script to auto start and stop. More info at

3 Likes