Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long running Jupyter notebook/lab?

I have Jupyter running in a tmux session on an ec2 instance. I have very long-running cells, but when I close my browser or laptop lid, the notebook no longer writes output cells (and may crash the python kernel).

This is how I launch labs on my remote instance:

jupyter lab --ip=0.0.0.0 --port=5002 --no-browser --allow-root

I'm looking for a solution to run a notebook indefinitely without losing data and without having to keep my local computer on.

  • I don't want to use a VNC or X-windows forwarding (too slow)
  • I don't want to rewrite my code into python scripts (need work only in jupyter labs)

There has got to be a solution out there!

Update:

The 'nohup' solution below doesn't work:

enter image description here

After running this cell and closing the browser, upon reopening there is no output:

enter image description here

like image 534
M.R. Avatar asked Feb 03 '23 17:02

M.R.


2 Answers

EDIT (after clarification):

You can use some Jupyter magic to continue running the cell after you close the browser or laptop, and then print the output after you return. Here's how that's done:

%%capture stored_output

import time
time.sleep(30)
print("Hi")

After returning, run the following:

stored_output.show()
# Hi

ORIGINAL:

You need to launch the notebook with nohup.

nohup jupyter notebook &

Adding the '&' is only needed to return the shell. The notebook will be running in the background and it's process ID won't be killed when you close your SSH connection.

like image 187
samredai Avatar answered Feb 06 '23 08:02

samredai


I don't think what you want is possible because when your laptop goes into sleep mode after closing the lid, the notebook client in your browser will stop interacting with the server, even if you managed to keep the SSH connection alive. You could change your OS settings to prevent your system from sleeping upon lid closing, but that would be no different than keeping your machine on and using the battery.

The approach I use is:

  • Start your remote Jupyter server in screen or tmux so the server process and Python kernel stay running if the SSH connection drops.
  • Write your long running cells in a way that your outputs either will not be written to stdout or will be logged into a file in the server.

When you awake your machine and restart the SSH connection, the cell will have finished running and you can inspect the results in another cell or look at your logs directly.

like image 24
foglerit Avatar answered Feb 06 '23 08:02

foglerit