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
pyton3 -m venv tvenv
source tvenv\bin\activate
pip install jupyter
jupyter notebook --generate-config
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
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.