【jupyter]】4、遠程服務器執行jupyter


常規是在本地筆記本電腦或是臺式機上,但是要有大量數據時,需要在遠程性能更強的服務器上運行;可能遠程服務器有圖形用戶界面,也可能沒有,大部分沒有的。常規的操作是在本地電腦上用一部分小數據,使用jupyter notebook把代碼寫對,然後jupyter nbconvert --to script your_notebook.ipynb轉換成.py文件,上傳到服務器上來執行全部更大的數據。代碼是可以運行,但不再能使用jupyter nobebook,無法再可視化運行結果。本文將寫如何在遠程服務器上使用jupyter。

1、連接遠程服務器

使用securecrt ,xshell等工具,或本地shell中用ssh來鏈接遠程服務器,或都直接用ssh(secure shell protocol)發種命令到遠程服務器,來開啓jupyter notebook。securecrt或xcell可以是windows,shell就必須是linux了,遠程服務器是linux.

ssh username:passwd@remote_server_ip commond
#我使用xshell,以前用securecrt效果一樣,連接後輸入:
jupyter notebook --no-browser --port=5008
#如果是ssh發送命令:
nohup ssh -f username:password@remote_server_ip "cd project_folder; conda activate tf1; jupyter notebook --no-browser --port=5008"
#一行有三個命令,用“;"隔開,這個命令會在端口5008打開jupyter notebook,並在遠程執行。-f 使得在後臺運行,nohup使靜默輸出

2、打開jupyter notebook

使用port forwarding,直接把遠程notebook作爲一個本地notebook打開
xshell設置方法,如下圖:
在這裏插入圖片描述
securecrt設置,options->session options->port forwarding->add如下圖:
在這裏插入圖片描述
ssh 輸入命令:

nohup ssh -N -f -L localhost:5008:localhost:5008 username:password@remote_server_ip
#-N 告訴ssh沒有遠程命令需要執行 -f使ssh在後臺執行,-L指定port forwarding

我使用的是xshell:
在這裏插入圖片描述
複製紅線所畫到本地瀏覽器就可以使用。

3、斷開連接

方法一:通過瀏覽器中,quit:
在這裏插入圖片描述
在xshell或securecrt中直接crtl+C也可以
方法二,命令行:

ssh username:password@remote_server_ip "jupyter notebook stop 5008"
#或
ssh username:password@remote_server_ip "pkill -u username jupyter"

4、便捷整個工作流程

這個主要是說一個linux本地機器連一個遠程linux機器,如果用securecrt 或xshell將不用,因爲它本身就很簡單
在~/.bashrc中填加:


alias port_forward='nohup ssh -N -f -L localhost:5008:localhost:5008 username:password@remote_server_ip'
alias remote_notebook_start='nohup ssh -f username:password@remote_server_ip "cd project_folder; conda activate tf1; jupyter notebook --no-browser --port=5008"; port_forward'
alias remote_notebook_stop='ssh username:password@remote_server_ip "pkill -u username jupyter"'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章