25

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?

BiBi
  • 7,418
  • 5
  • 43
  • 69
Michael D
  • 1,711
  • 4
  • 23
  • 38
  • In practice, long running notebooks fail. My suggestion is that you convert the notebook to a script, then run it using screen as @MichaelD suggests. I know this doesn't answer the OP's question, but it will solve their problem – shallow_water Jan 16 '19 at 03:11

5 Answers5

36

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.
Michael D
  • 1,711
  • 4
  • 23
  • 38
BiBi
  • 7,418
  • 5
  • 43
  • 69
  • 1
    To reconnect, you just have to reconnect to the remote server using `ssh -L xxxx:localhost:yyyy server`. If you want to access the screen where the jupyter is being run, you can do `screen -r`. Note that reconnecting to the remote server is sufficient, you don't have to reopen the screen. – BiBi Aug 23 '17 at 11:33
  • I use tmux for persistent running of programs. – Jingpeng Wu Dec 05 '18 at 14:54
  • 1
    what is `xxxx` and `yyyy`. Can you explain about the ports to assign?` – maninekkalapudi Feb 03 '19 at 07:56
  • 1
    Basically, you should use an unused port on the local machine (`xxxx`) and the remote machine (`yyyy`). For instance, `xxxx` could be `8888` and `yyyy` could be also `8888`. – BiBi Feb 03 '19 at 10:07
11

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

desertnaut
  • 57,590
  • 26
  • 140
  • 166
1

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Bouke
  • 145
  • 3
  • 8
0

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

jns
  • 1,250
  • 1
  • 13
  • 29
0

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 username@xx.xx.xx.xx. If port needs to be specified, you can use ssh -NL 8085:localhost:8085 -p xxxx username@xx.xx.xx.xx
  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.

Jayakrishnan
  • 563
  • 5
  • 13