Michael D
Michael D

Reputation: 1747

Persistent use of Jupyter Notebook from remote server

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

Answers (6)

olenscki
olenscki

Reputation: 507

As an alternative use tmux:

  1. Install tmux with conda install tmux (it may be necessary to install ncurse and libserve);
  2. Create a session tmux new -s jupyter_session;
  3. This will create and activate a new tmux session, which is persistent;
  4. Instantiate the jupyter lab server jupyter lab --no-browser --port=8888 (use the port you're tunneling from);
  5. Use Ctrl+B and D to dettach from the tmux session. This will "close" the session for you, but it will be still running;
  6. To re-attach tmux attach -t jupyter_session

Additionally: enable automatically saving widget states on jupyter in Settings > Save Widget States Automatically

Upvotes: 0

Jayakrishnan
Jayakrishnan

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.

  1. In remote server 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
  2. In local 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]
  3. In browser 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

BiBi
BiBi

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.

  1. On your computer: ssh -L xxxx:localhost:yyyy server.
  2. screen.
  3. jupyter notebook --no-browser --port=yyyy. [on remote server]
  4. In your browser: localhost:xxxx.

To disconnect manually and reconnect:

  1. Exit the screen window: control + a and then d.
  2. Disconnect from the server: control + d
  3. And reconnect ssh -L xxxx:localhost:yyyy.
  4. Optionally, you can reopen the screen window, though unnecessary, using screen -r.
  5. Go back to your notebook or reopen localhost:xxxx.

Upvotes: 37

jns
jns

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

Bouke
Bouke

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

desertnaut
desertnaut

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

Related Questions