英文:
How to start Jupyter Notebook on a GCP VM with predefined token automatically on VM start?
问题
我已在GCP虚拟机上设置了Jupyter Notebook,并希望每次启动VM时都能自动启动,使用预定义的令牌(操作系统为CentOS)。
有人知道如何解决这个最后的问题,以便我们可以在用户test1下每次启动VM时启动Jupyter Notebook吗?
到目前为止,我已经完成以下工作:
- 创建了一个本地的test1用户,并为其创建了一个虚拟环境,在虚拟环境中安装了Jupyter Notebook,以隔离Python环境
python3 -m venv tvenv
source tvenv/bin/activate
pip install jupyter
- 明白我需要生成配置文件并在配置文件中更新令牌/ IP /端口等信息,因为无法使用动态生成的令牌
jupyter notebook --generate-config
- 修改了NotebookApp.token/NotebookApp.port/NotebookApp.ip等属性以使其正常工作。
- 基于现有的问题创建了一个shell脚本,该脚本将添加到GCP VM的启动中
sudo -u test1 bash -c 'cd ~/; nohup tvenv/bin/jupyter-notebook &'
当我从root手动运行该命令时,它可以正常工作,但当我在GCP启动脚本中更新时,它不断失败,并显示以下消息:
帐户或密码已过期,请重置密码然后重试
注意:有用的调试提示。您可以在VM内部运行以下命令以获取启动脚本日志:
sudo journalctl -u google-startup-scripts.service
英文:
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
- 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
- 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
- modified NotebookApp.token/NotebookApp.port/NotebookApp.ip etc attributes to get that to work.
- 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
答案1
得分: 0
如John在评论中已经指出的,sudo不是一个好的选项,因为启动脚本以root身份运行,可以使用su来切换到另一个配置文件。还有一些其他的事情你应该知道,因为你正在使用虚拟环境,所以你必须设置类似于'activate'的环境变量。
你可以尝试的一个可能的解决方案如下:
su -c 'PATH="/home/test1/tvenv/bin:$PATH";export PATH; cd ~/; nohup tvenv/bin/jupyter-notebook &' -s /bin/sh test1
它的作用是设置虚拟环境的路径(与activate等其他任务一起完成的活动),切换到用户的主目录,并在test1用户配置文件下以后台方式运行笔记本。
我还建议你在gcp虚拟机中使用关机脚本来优雅地关闭笔记本,请确保用你在配置中使用的端口替换端口。
关于启动,你可以使用以下命令来检查关机日志:
sudo journalctl -u google-shutdown-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论