Reputation: 1747
I connect to a remote server using ssh -L
but if I close the laptop lid or the connection is lost, the jupyter notebook is disconnected.
After I reconnect to the remote server, the "last" session is lost.
What can be done to make it persistent?
Could screen
help with it?
Upvotes: 27
Views: 26909
Reputation: 507
As an alternative use tmux
:
tmux
with conda install tmux
(it may be necessary to install ncurse
and libserve
);tmux new -s jupyter_session
;tmux
session, which is persistent;jupyter lab --no-browser --port=8888
(use the port you're tunneling from);Ctrl+B
and D
to dettach from the tmux session. This will "close" the session for you, but it will be still running;tmux attach -t jupyter_session
Additionally: enable automatically saving widget states on jupyter in Settings > Save Widget States Automatically
Upvotes: 0
Reputation: 671
Use the nohup command to keep jupyter running even after exiting the shell or terminal. Type the following command in the specified locations.
nohup jupyter notebook --no-browser --port=8085 > my.log 2>&1 < /dev/null &
. This runs jupyter in port 8085 and any stdout would be present in my.log
ssh -NL 8085:localhost:8085 [email protected]
. If port needs to be specified, you can use ssh -NL 8085:localhost:8085 -p xxxx [email protected]
http://127.0.0.1:8085/
Sometimes port 8085 may be occupied in the remote server, in such cases try it with another port but make sure you use the same port number in the local while tunneling.
Upvotes: 0
Reputation: 7908
On the remote server, you should open your jupyter in a screen
session, it will make it persistent if you lose the connection to the server and resume it.
ssh -L xxxx:localhost:yyyy server
.screen
.jupyter notebook --no-browser --port=yyyy
. [on remote server]localhost:xxxx
.To disconnect manually and reconnect:
control + a
and then d
.control + d
ssh -L xxxx:localhost:yyyy
.screen -r
.localhost:xxxx
.Upvotes: 37
Reputation: 1362
BiBi's answer is correct. But I had cases where my ssh connection terminated unexpectedly and the port forwarding no longer worked when trying to reconnect. Probably there was some dangling process on the remote machine, not sure.
Anyway, in these cases I used socat
to proxy between two local ports on the remote machine:
# jupyter notebook/lab running in screen on port yyyy, then your connection dies...
ssh -L xxxx:localhost:zzzz
socat tcp-listen:zzzz,reuseaddr,fork tcp:localhost:yyyy
This way you can avoid restarting jupyter on a different port
Upvotes: 0
Reputation: 185
Adding to @BiBi's answer...
Instead of screen
I could recommend you to take a look at tmux. Especially, if you combine tmux with the Tmux Plugin Manager and install Tmux Resurrect, even after reboots of your remote server you will be able to go back to your previous Tmux sessions.
Shortcuts for tmux are somewhat equal to those of screens, just that control + a
is replaced by control + b
. Of course, tmux allows you to configure your custom shortcuts.
Upvotes: 1
Reputation: 60319
The standard usage for persisting Jupyter server sessions is the use of nohup
and &
; in your remote server with IP address xx.xx.xx.xx
:
nohup jupyter notebook --no-browser --ip xx.xx.xx.xx --port yyyy &
Now, even if you switch off your laptop or lose the connection, you will be always able to reconnect by pointing your browser at xx.xx.xx.xx:yyyy
Upvotes: 11