Slurm远程登录Jupyter Notebook

前言

  • 之前想利用实验室的服务器资源(GPU和CPU)来运行jupyter notebook,但是现在实验室使用slurm管理所有的节点,ssh只能登录管理节点,再通过管理节点来申请资源或提交任务,因此不能通过ssh来直接访问计算资源
  • 不能直接在管理节点上启动jupyter,可以通过slurm脚本通过sbatch发送上去启动jupyter,或者通过srun申请一个交互的bash,在其中启动jupyter

实施

交互bash启动jupyter

在服务器上启动jupyter

srun -w sugon-gpu-x -c 4 --gres=gpu:V100:1 --pty bash //申请资源
source activate env_dl //启动环境
jupyter notebook --no-browser --port=8879 --ip=sugon-gpu-x //启动jupyter,指定port,指定ip
//目前服务器每个用户对节点的占有时间有限制,另外为了防止断连,tmux或nohup的使用自便

在本地连接jupyter

//8880为本地端口,sugon-gpu-x:8879为服务器jupyter指定ip和指定端口,your_name为用户名,cluster_url为服务器ip
ssh -N -L 8880:sugon-gpu-x:8879 your_name@cluster_url 

打开浏览器通过 localhost:8880 访问

// 对于浏览器要求的token,复制启动jupyter后返回的信息中的token即可
"http://sugon-gpu-x:8879/?token=d5a9744d7ebf58fec4ebf133c******27fd74fb12bb896"

Slurm脚本启动jupyter

参考:分享脚本远程登陆 Jupyter Notebook 通过sbatch发射脚本上去启动jupyter 脚本内容:

#!/bin/bash
#SBATCH --partition sugon
#SBATCH --nodes 1
#SBATCH --ntasks 1
#SBATCH --cpus-per-task 4
#SBATCH --gres=gpu:V100:1
#SBATCH --time 48:00:00
#SBATCH --job-name jupyter-notebook
#SBATCH --output ./Logs/jupyter-notebook-%J.log
#SBATCH --error ./Logs/jupyter-errors.log
#SBATCH [email protected]

# get tunneling info
XDG_RUNTIME_DIR=""
port=$(shuf -i8000-9999 -n1) # 可以固定下来
node=$(hostname -s)
user=$(whoami)
cluster=$(hostname -f | awk -F"." '{print $2}')

### 在这里添加你的服务器地址
clusterurl="192.168.5.xxx"

export PATH=$PATH:~/.local/bin

# print tunneling instructions jupyter-log
echo -e "
MacOS or linux terminal command to create your ssh tunnel:
ssh -N -L ${port}:${node}:${port} ${user}@${clusterurl}
 
 Here is the MobaXterm info:

 Forwarded port:same as remote port
 Remote server: ${node}
 Remote port: ${port}
 SSH server: ${cluster}.${clusterurl}
 SSH login: $user
 SSH port: 22

 Use a Browser on your local machine to go to:
 localhost:${port} (prefix w/ https:// if using password)

 or copy the token from the error file"

 # load modules or conda environments here
 # e.g. farnam:
 # module load Python/2.7.13-foss-2016b 
 # conda env activate mx
 # DON'T USE ADDRESS BELOW. 
 # DO USE TOKEN BELOWa

 source activate env_dl
 jupyter-notebook --no-browser --port=${port} --ip=${node}

之后查看Logs/文件夹下的log文件和error文件,从log文件中找到本地连接方式(与前面的相同),从error文件中找到token

ssh -N -L xxxx:sugon-gpu-x:xxxx your_name@cluster_url

猜你喜欢

转载自www.cnblogs.com/cookielbsc/p/12411560.html