Running Jupyter Notebook in Background Mode on a Remote Server
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 andip
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.
Open a new terminal on your local machine.
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:
Run the command
jupyter notebook list
on the remote server to get the port number used by Jupyter Notebook.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.
Subscribe to my newsletter
Read articles from Anant Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Anant Mishra
Anant Mishra
I am a passionate developer with a strong interest in machine learning, deep learning, and research. My journey in the field of technology has been exhilarating, constantly pushing me to explore the boundaries of what is possible.