如何在 linux ubuntu 使用 tmux 讓 JupyterLab 在背景運行的流程?
使用 tmux
可以讓 JupyterLab 在背景運行,即使 SSH 斷線、終端退出 (exit
),甚至系統重啟後,JupyterLab 仍然可以繼續運作。以下是詳細的流程:
✅ 步驟 1:安裝 tmux
如果你的系統還沒有 tmux
,先安裝它:
# Ubuntu / Debian apt-get update && apt-get install -y tmux # CentOS / RHEL yum install -y tmux # Alpine Linux(Docker 常用) apk add tmux
✅ 步驟 2:啟動 tmux
會話
啟動一個新的 tmux
會話,名稱為 jupyter
:
tmux new -s jupyter
這樣你會進入一個新的 tmux
視窗,就像開了一個新的終端視窗。
✅ 步驟 3:在 tmux
內啟動 JupyterLab
在 tmux
內執行:
jupyter lab --ip=0.0.0.0 --port=8888 --allow-root
這樣 JupyterLab 會在 tmux
內運行。
✅ 步驟 4:斷開 tmux
(讓 JupyterLab 在背景運行)
如果你想讓 JupyterLab 繼續運行,但不影響你的終端,可以按:
Ctrl + B,然後按 D
這樣你會「退出 tmux
會話」,但 JupyterLab 仍然在背景運行!
✅ 步驟 5:重新連回 tmux
如果你想重新連回 tmux
會話(例如你 SSH 重新連線後),可以用:
tmux attach -t jupyter
這樣你會回到之前運行 JupyterLab 的環境。
✅ 步驟 6:列出所有 tmux
會話
如果你忘了 tmux
會話名稱,可以列出所有 tmux
會話:
tmux ls
它會顯示:
jupyter: 1 windows (attached)
然後你可以用:
tmux attach -t jupyter
✅ 步驟 7:停止 JupyterLab
如果你想要關閉 JupyterLab,連回 tmux
之後,執行:
exit
或者:
pkill -f jupyter
這樣 JupyterLab 會完全停止。
總結
步驟 | 指令 |
---|---|
安裝 tmux |
apt-get install -y tmux |
建立 tmux 會話 |
tmux new -s jupyter |
啟動 JupyterLab | jupyter lab --ip=0.0.0.0 --port=8888 --allow-root |
斷開但保持運行 | Ctrl + B → D |
重新連接 tmux |
tmux attach -t jupyter |
列出 tmux 會話 |
tmux ls |
停止 JupyterLab | exit 或 pkill -f jupyter |
這樣 JupyterLab 不管 SSH 斷線、終端退出 (exit
),都能持續運行,讓你安心使用!
留言板
歡迎留下建議與分享!希望一起交流!感恩!