Keep Jupyter Notebook TQDM output after detaching tmux session - jupyter-notebook

I am running a jupyter notebook within a tmux session. The Jupyter Notebook uses a simple countdown timer and tqdm progress bar to test if I can detach the tmux session and retain the tqdm output.
Here is how I set up the tmux session:
tmux new-session -s mysession
Then I open a Jupyter Notebook within the tmux session
jupyter notebook
Create a .ipynb notebook with the following test code:
from tqdm import tqdm
import time
with tqdm(total=10000) as pbar:
for i in range(10000):
time.sleep(1)
pbar.update(1)
print('DONE')
Once this is running, I can see the counter and progress bar:
Now detach the session and close the open Jupyter Notebook browser tabs
Ctrl-b d
Reopen the Jupyter Notebook in browser with tmux session detached
http://localhost:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
When I reopen the browser, the Jupyter Notebook is running, although the tqdm output is stopped at wherever I closed the Jupyter Notebook browser windows.
Is there a way to keep output such as tqdm progress bar information running when reopening a Jupyter Notebook from a detached tmux session?

Related

Connecting Colab to Local Runtime Jupyter Notebook

I'm trying to connect my Google Colab to a local runtime via Jupyter Notebook. There is one part I can't figure out, which is this:
#Ensure that the notebook server on your machine is running on port 8888 and accepting requests from https://colab.research.google.com.
jupyter notebook \
--NotebookApp.allow_origin='https://colab.research.google.com' \
--port=8888 \
--NotebookApp.port_retries=0
I tried copy-pasting it into my anaconda prompt but only "jupyter notebook " is pasted and executed. How do you get all that code typed into prompt? Is it some cmd feature that I'm completely oblivious to?
The command you are currently running should be run on a Linux machine. If you have a Windows machine, replace the \ at the end of each part of the command with ^. So, your command will be
jupyter notebook ^
--NotebookApp.allow_origin='https://colab.research.google.com' ^
--port=8888 ^
--NotebookApp.port_retries=0
Or, the complete command with all the parameters can be run in a single line like this
jupyter notebook --NotebookApp.allow_origin='https://colab.research.google.com' --port=8888 --NotebookApp.port_retries=0
I have tested this to work on the command prompt, and the local runtime connection is successful.

how to shutdown Jupyter Notebook When there is not activity

jupyter notebook:
[Config option `shutdown_no_activity_timeout` not recognized by `NotebookApp`.]
If you are running in a linux base. Open the terminal and do the folowing:
ps -e | grep jupyter (look at the pid of the jupyter process)
kill -9 pid (the pid of the previous jupyter process)

How to run jupyter notebook in the background ? No need to keep one terminal for it

Often we run jupyter notebook to pop up a page in browser to use notebook. However, the terminal opening the server remains there. Is there a way that we can close that terminal with server running in the back ?
You can put the process into the background by using jupyter notebook --no-browser & disown. You can close the terminal afterwards and the process will still be running.
If you're using zsh you can also use a shorter version that does the same: jupyter notebook --no-browser &!.
To kill the process you can use pgrep jupyter to find the PID of the process and then kill 1234, replacing 1234 with the PID you just found.
Explanation
The --no-browser flag makes jupyter not open the browser automatically, it also works without this flag.
The & puts it into the background of the currently running shell.
The disown then removes the job from the background of the currently running shell and makes it run independently of the shell so that you may close it.
In the zsh version the &! is a built-in function that does the same as & disown.
Tmux is a good option available to run your Jupyter Notebook in the background.
I am running my Jupyter Notebook on a google cloud platform's VM instance with OS: Ubuntu 16.0. Where
I have to start SSH terminal
Then run the command: jupyter-notebook --no-browser --port=5000. It will start the Jupyter Notebook on port number 5000 on that VM instance
Then I open my browser and typer ip_addrees_of_my_instance:port_number which is 5000. It will open my Notebook in the browser.
Now up to this, all is good. But wait if the connection with my SSH terminal is terminated then the Jupyter Notebook stops immediately and hence I have to re-run it once again once the ssh terminal is restarted or from new ssh terminal.
To avoid this tmux is very good option.
Terminal Multiplexer (tmux) to Keep SSH Sessions Running in the background after ssh terminal is closed:
Start the ssh terminal
Type tmux. It will open a window in the same terminal.
Give command to start Jupyter Notebook here. Open Notebook.
Now if SSH terminal is closed/terminated it will keep running your notebook on the instance.
If the connection terminated then:
reconnect or open new ssh terminal. To see this Jupyter Server(which is kept running in the background) type: tmux attach command.
(Edited: changed "notebook" to "Jupyter Server")
Want to terminate tmux session:
Close the notebook. Then type exit in tmux-terminal-window.
(update: If we close the notebook and use tmux detach command: it will exit from tmux session window/terminal without terminating/stopping the tmux sessions)
For more details please refer to this article: https://www.tecmint.com/keep-remote-ssh-sessions-running-after-disconnection/
Under *nix, the best way to run a program avoiding to be terminated by closing the terminal is to use nohup (no Hang up).
To start browser after running the server use the command:
nohup jupyter notebook &
And to start the server without opening the browser use the command:
nohup jupyter notebook --no-browser &
Note that you can shut down the jupyter server by using Quit in the upper right of the page of jupyter.
nohup puts as a parent of the process init(0), so it will not receive the "Hang Up" signal when the terminal is closed. All the output (standard output and standard error) are redirected to the file nohup.out
nohup exists both as program and shell command, so if you have bash, check man page of bash to read more details.
This works for me when running a jupyter notebook server in the background.
$> nohup jupyter notebook --allow-root > error.log &
Stop the nohup jupyter notebook is simple.
First, find the pid of jupyter:
$> ps -ef| grep jupyter
e.g output like:
root 11417 2897 2 16:00 pts/0 00:04:29 /path/to/jupyter-notebook
Then kill the process:
$> kill -9 11417
You can also simplify this by storing the pid with:
$> nohup jupyter notebook --allow-root > error.log & echo $!> pid.txt
i.e, you can stop the notebook with:
$> kill -9 $(cat pid.txt)
An alternative way to stop the jupyter notebook is quit from the notebook page.
You can use screen to run it.
screen -A -m -d -S anyscreenname jupyter notebook --no-browser
This will start jupyter in a screen and you can access screen using screen commands.
Actually, jupyter notebook & alone is not enough, the backend will still log to your screen.
What you need is, cited from this issue
jupyter notebook > /path/to/somefileforlogging 2>&1 &
You can start up the notebook in a screen or tmux session. Makes it easy to check error messages, etc.
For remote machines jupyter notebook & works fine.
However, it does not work on local machines when you close the terminal.
For local machines use tmux.
Not real sophisticated but it gets the job done:
#! /bin/bash
#probably should change this to a case switch
if [ "$1" == "end" ]
then
echo
echo "Shutting Down jupyter-notebook"
killall jupyter-notebook
echo
exit 0
fi
if [ "$1" == "-h" ]
then
echo
echo "To start : jnote <port> [default 8888]"
echo "To end : jnote end"
echo "This help : jnote -h"
echo
exit 0
fi
#cast from string
PORT=$(($1))
RETURN=0
PID=0
if [ "$PORT" == "0" ] || [ "$PORT" == "" ]; then PORT=8888; fi
echo
echo "Starting jupyter-notebook"
#background and headless, set port, allow colab access, capture log, don't open browser yet
nohup jupyter notebook \
--NotebookApp.allow_origin='https://colab.research.google.com' \
--port=$PORT --NotebookApp.port_retries=0 \
--no-browser >~/jnote.log 2>&1 &
RETURN=$?
PID=$!
#Wait for bg process to complete - add as needed
sleep 2
if [ $RETURN == 0 ]
then
echo
echo "Jupyter started on port $PORT and pid $PID"
echo "Goto `cat ~/jnote.log | grep localhost: | grep -v NotebookApp`"
echo
exit 0
else
echo
echo "Jupyter failed to start on port $PORT and pid $PID with return code $RETURN"
echo "see ~/jnote.log"
echo
exit $RETURN
fi
jupyter notebook & >> disown
put the process into the background by using jupyter notebook &
then type disown or disown <the process id>
you can close the terminal now
Detach Jupyter process from the controlling terminal and send all its input and output data to /dev/null which is a special device file that writes-off any data written to it.
jupyter notebook </dev/null &>/dev/null &
Lazy people like me would prefer to edit ~/.bash_aliases and create an alias:
alias jnote='jupyter notebook </dev/null &>/dev/null &'
Reference: https://www.tecmint.com/run-linux-command-process-in-background-detach-process/
As suggested by one of the users, using
jupyter notebook &
solves the issue. Regarding the comments stating that it kills the kernel after closing the terminal, probably you are using
jupyter-notebook &.
If you are using iTerm2 software,
first you need to set:
brew shellenv
Then start jupyter in nohup:
eval $(/usr/local/bin/brew shellenv)
nohup jupyter notebook &
Here is a command that launches jupyter in background (&) detached from the terminal process (disown) and without opening the browser (--no-browser)
No log will be shown on the terminal since they are redirected (&>) to a file jupyter_server.log so they can still be referred to later.
jupyter notebook --no-browser &> jupyter_server.log & disown
If you don't wan't to store the logs(discouraged):
jupyter notebook --no-browser &> /dev/null & disown
Thanks to the other answers this one is built upon: here and there

How to RE-connect to a Remote Jupyter instance (Running Code)

I have several long running scripts (in Jupyter Notebook) on a remote Google Cloud Compute Instance.
If I lose the ssh connection, I cannot reconnect to the (running) Notebook without stopping those running scripts--executing within the Notebook.
It seems that closing my macbook, will sever my connection to the remote (running) jupyter notebook. Is there some way to reconnect without stopping the script?
On Google Cloud, Jupyter is still running. I just can't connect to the notebook executing the code––without stopping code execution.
I'm sure other Jupyter users have figured this out :)
Thanks in advance
My GCloud Tunneling Script
gcloud compute ssh --zone us-central1-c my-compute-instance -- -N -p 22 -D localhost:5000
Bash Script that Launches Chrome
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
"localhost:22"
--proxy-server="socks5://localhost:5000"
--host-resolver-rules="MAP * 0.0.0 , EXCLUDE localhost"
--user-data-dir=/tmp/
Nohup that launches Jupyter on Gcloud
nohup jupyter notebook --no-browser > log.txt 2>&1 &
On my Sierra-os macbook, no proxy settings (System Preferences) are enabled
On Google Cloud, I'm NOT using a static ip, just an ephemeral ip.
Much appreciation in advance
What do you mean by "cannot reconnect" ? Do you mean you can't see the notebook interface anymore ? (In which case this is likely a google cloud question).Or do you mean you can't run code or see previous results ?
If the second, this is a known issue, the jupyter team is working on it; The way to go around that is to wrap your code in Python Futures, that store intermediate code; thus re-accessing the future will not trigger re-computation, but will show you intermediate results.

How can I return to command line with jupyter notebook running?

I am connected to a Linode terminal via SSH.
I have started a Jupyter Notebook server from the command line.
But I want to do some other tasks in the command line, keeping the NB server running. I can't work out how to do this without stopping the Jupyter Notebook server. It says:
[I 05:55:05.523 NotebookApp] Use Control-C to stop this server and shut
down all kernels (twice to skip confirmation).
Since you mentioned Linode, I'm assuming it is on a linux/unix system.
Possbile options:
Send process to background
send process to background: jupyter notebook &
do some work on terminal
close jupyter with pkill jupyter (this should have the same effect as
a ctrl+c keypress)
WARNING: this will kill all jupyter instances
use a terminal multiplexer like tmux
just use multiple ssh sessions (not really elegant imo)
Overall, I think the option that would be easiest and most flexible is option 2.

Resources