gpt4 book ai didi

ubuntu - 从远程服务器运行 jupyter 笔记本的脚本

转载 作者:行者123 更新时间:2023-12-04 18:26:08 26 4
gpt4 key购买 nike

我有一台运行 jupyter 笔记本的服务器(Ubuntu 服务器 16.04)和一台本地机器(Mac),我使用 google-chrome 来可视化这些笔记本。为此,我必须:

  • 在服务器中运行 jupyter notebook:

    jupyter notebook --no-browser --port=${remotePort}
  • 在我的本地机器上指定一个 SHH 隧道:

    ssh -f ${username}@${serverIP} -L ${localPort}:localhost:${remotePort}

  • 为了自动化这个过程,我创建了脚本 jupyter.sh(如下所述),这样我只在本地机器上运行:
    bash jupyter.sh -u myUserNameInServer

    它完美无缺。它能够运行前两个步骤,而且它会自动在我的网络浏览器中打开 jupyter 页面。不过,我想知道是否有更好的方法来做到这一点。我将非常感谢您的意见。

    提前致谢。
    #######################################################################
    ## 1. SET VARIABLES TO STABLISH THE SSH CONNECTION
    # Get username from command line: bash jupyter.sh -u username
    while [[ $# -gt 1 ]]
    do
    key="$1"
    case $key in
    -u|--username)
    username="$2"
    shift # past argument
    ;;
    esac
    shift # past argument or value
    done
    # Specificy other variables to stablish the ssh connection
    localPort=8890
    browser="Google Chrome"
    serverIP=the_IP_of_the_server
    #######################################################################
    # 2. RUN JUPYTER IN REMOTE SERVER
    out=$(ssh -T ${username}@${serverIP} <<HERE
    # Only run jupyter if it isn't already running
    if [ \$(ps -u ${username} | grep jupyter | wc -l) -eq 0 ]
    then
    # Create a folder called jupyter, and move into it
    if [ ! -d jupyter ]; then mkdir jupyter; fi
    cd jupyter
    # Create a script to run jupyter
    echo "jupyter notebook --no-browser --NotebookApp.token=${username}" > jupyter.sh
    # Run jupyter in the background
    screen -S jupyter -d -m bash jupyter.sh
    fi
    # Output the remote port number. If there is more than 1, get the first one
    jupyter notebook list | grep localhost | awk '{split(\$0,a,"localhost:");split(a[2],b,"/"); print b[1]}' | head -n1
    HERE
    )

    #######################################################################
    # 3. SET SSH TUNNEL
    # Pass the remote port to a variable in the local machine
    remotePort=$(echo $out | awk '{print $NF}')
    # Start listening in local port 8890 if that port isn't already in use
    # num equal 1 if port number is already in use, 0 otherwise
    num=$(netstat -lnt | awk 'BEGIN{x=0} ($6 == "LISTEN" && $4 ~ "8890$"){x=1}END{print x}')
    if [ $num -eq 0 ]
    then
    ssh -f ${username}@${serverIP} -L ${localPort}:localhost:${remotePort} -N
    fi
    #
    # Open jupyter in browser
    open -a "${browser}" http://localhost:${localPort}/tree?token=${username} &

    最佳答案

    您可以使用默认情况下未激活的“jupyter 配置文件”,因此需要先执行此命令(在您的服务器上):

    jupyter notebook --generate-config

    然后在“/.jupyter”文件夹中生成的“jupyter_notebook_config.py”中:
    取消注释该行并更改值。

    通过这种方式,您可以调整配置,例如密码而不是 token 、默认目录、端口等.. 随心所欲地做这可能是在脚本中执行一些“jupyter 配置”然后保留其他所有内容的正确方法。

    关于ubuntu - 从远程服务器运行 jupyter 笔记本的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43696291/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com