Search code examples
linuxgoogle-cloud-platformvirtual-machine

How to start Jupyter Notebook on a GCP VM with predefined token automatically on VM start?


OK I have set up jupyter notebook on a gcp VM, I want it to start automatically every time VM is started with a predefined token. (OS in Centos)

Does any one have any idea how to solve this final issue so that we can get the jupyter notebook started under the user test1 , everytime the VM starts ?

Here is what I have done so far

  1. Created a local test1 user and created a Virtual env for it, installed jupyter notebook inside the virtual env for isolation of python
pyton3 -m venv tvenv
source tvenv\bin\activate
pip install jupyter
  1. Understood that I would need to generate config and update token / ip / port etc inside the config becuase dynamically generated token cannot be used
jupyter notebook --generate-config 
  1. modified NotebookApp.token/NotebookApp.port/NotebookApp.ip etc attributes to get that to work.
  2. based on an existing question created a shell script to be added to the start up to the gcp VM
sudo -u test1 bash -c 'cd ~/; nohup tvenv/bin/jupyter-notebook &'

When i run the command manually from root it works when I update in GCP startup it constantly fails saying

Account or password is expired, reset your password and try again

Note : Helpful debugging tip. You can get the startup script logs by running below command once inside the VM

sudo journalctl -u google-startup-scripts.service

Solution

  • As John has already pointed out to you in the comment, sudo is not a good option, since the Startup scripts run as root, su can be used to switch to another profile. Few other things that you should be aware of is that as you are using virtual enviornment, you have to set up similar enviornment variables as 'activate' does.

    one possible solution that you can try is below

     su -c 'PATH="/home/test1/tvenv/bin:$PATH";export PATH; cd ~/; nohup tvenv/bin/jupyter-notebook &' -s /bin/sh test1
    

    What it does is sets the path to the virtual enviornment ( activity that is done via the activate among other tasks ) moves to home directory of the user and runs the notebook in background with test1 user profile.

    I would also suggest that you use shutdown script in the gcp vm to shutdown notebook gracefully, make sure that you replace the port with the port that you are using in your config

    jupyter notebook stop 8888
    

    Similar to start up you can check the shutdown logs by using the command below

    sudo journalctl -u google-shutdown-scripts.service
    

    Let me know if you still face any issues.