Useful Jupyter Notebook Finder Shell Script


Tidier Than Normal


One of my bad habits, whether it is in a browser, text editor or a terminal, is to have far too many tabs open. Normally I’ll hit a limit and go on a tab-closing frenzy. This can become problematic when I’m running a Jupyter Notebook server from the terminal, and I lose the console tab after having closed the localhost browser tab. If I’m organised, I’ll be running one or two, at localhost:8888 and localhost:8889, but if I’ve been going a while we can get in a bit of a mess.

Apparently I’m not the only one, and on the LSST Slack, Robert Lupton suggested the following code snippet to sniff out notebooks:

#!/bin/bash
#
# Show the paths to currently running jupyter notebooks
#
for pid in $(ps lx | awk '/jupyter/ && /python/ && !/awk/ {print $2}'); do
      echo $(lsof -P -p $pid | awk '/ DIR / || /LISTEN/ { print $9 }' | uniq)
done

Which is pretty handy!

But…

In the process of playing around with this I realised that this functionality was already baked into my jupyter. Navigating to http://127.0.0.1:8888 threw this at me, rather than the tree I was expecting:

Jupyter Thought of This!

So they got there first - and with a reminder about the neat token auth features to boot. But there is a consolation - the shell script does seem faster! Additionally, if you really need to kill the process dead, you can get the PID too -

#!/bin/bash
#
# Show the paths to currently running jupyter notebooks
#
echo "pid: " $pid $(lsof -P -p $pid | awk '/ DIR / || /LISTEN/ { print $9 }' | uniq)
      echo $(lsof -P -p $pid | awk '/ DIR / || /LISTEN/ { print $9 }' | uniq)
done