Running Jupyter Notebook in Background Mode on a Remote Server

Running Jupyter Notebook in Background Mode on a Remote Server

Photo by Reka Illyes on Unsplash

Jupyter Notebook is a powerful tool for interactive computing. In some cases, you may need to run Jupyter Notebook on a remote server and access it from your local machine. This can be useful when working with large datasets or training deep learning models that require more computational resources.

In this tutorial, we will guide you through the process of running Jupyter Notebook in the background on a remote server and connecting to it from your local machine.

Setting up the Remote Connection

  • Log in to the remote server using SSH. Replace user with your username and ip with the IP address or hostname of the remote server.
ssh user@ip
  • Start Jupyter Notebook in the background on the remote server. Use the nohup command to prevent the notebook from being terminated when you log out. Specify the desired port (e.g., 8888) using the --port option. The --no-browser option ensures that the notebook does not open a web browser.
nohup jupyter-notebook --no-browser --port=8888 &

The logs of the Jupyter Notebook will be stored in a file called nohup.out, which is located in the same folder where you executed the above command.

Open a web browser on your local machine and navigate to localhost:8888. You should now be able to access the Jupyter Notebook running on the remote server.

Note

If you disconnect from the current Jupyter session and then reconnect, you won’t be able to see the output of the currently running cell. You will only see the output generated after you reconnect.

Bonus Advice 🎁

If you are training a deep learning model, use CSVLogger in callbacks, and use this command to see the output from the CSV file.

cat model.csv | column -t -s, | less -S

Reconnecting after Disconnecting

If you get disconnected from the remote server, follow these steps to reconnect to the running Jupyter Notebook.

  1. Open a new terminal on your local machine.

  2. Re-establish the secure tunnel to the remote server using the following command:

ssh -NL 8888:localhost:8888 remote_user@remote_ip

Open a web browser and navigate to localhost:8888 to access the Jupyter Notebook.

Stopping the Running Jupyter Notebook

If you want to stop the Jupyter Notebook running on the remote server, follow these steps:

  1. Run the command jupyter notebook list on the remote server to get the port number used by Jupyter Notebook.

  2. Use the lsof command to find the PID (process ID) associated with the Jupyter Notebook process on that port. Replace <port-number> with the actual port number obtained from the previous step.

lsof -n -i4TCP:<port-number>

Kill the Jupyter Notebook process by running the kill command with the appropriate PID. Replace <PID> with the actual process ID obtained from the previous step.

kill -9 <PID>

This completes the process of running Jupyter Notebook in the background on a remote server and accessing it from your local machine. Remember to adapt the commands based on your specific setup.