Skip to content

Instantly share code, notes, and snippets.

@spestana
Last active April 16, 2019 19:48
Show Gist options
  • Save spestana/1c4cbb03f8a4268210885b1a36b12bfb to your computer and use it in GitHub Desktop.
Save spestana/1c4cbb03f8a4268210885b1a36b12bfb to your computer and use it in GitHub Desktop.
template for starting and connecting to a jupyter notebook on a remote server
#!/bin/bash
# Start jupyter notebook on the remote server:
ssh -n -f <USERNAME>@<SERVER> "sh -c 'nohup jupyter notebook --port=8888 > /dev/null 2>&1 &'"
# Find the URL with port number and token:
url=$(ssh -l <USERNAME> <SERVER> 'grep -E -oh 'http:\/\/localhost:[0-9]{4}\/tree.token=[a-z0-9]{48}' '/run/user/<userid>/jupyter/nbserver-*-open.html' | head -1')
# Get the port number from the URL:
port=$(echo $url | grep -E -o 'localhost:[0-9]{4}' | cut -d: -f2)
# Start connection with jupyter notebook server:
ssh -f -L $port:localhost:$port -N <USERNAME>@<SERVER>
# Open the page URL and apply the token to log in:
start $url
@spestana
Copy link
Author

spestana commented Apr 16, 2019

Replace <USERNAME>, <SERVER> and <userid>.

The userid and path to the nbserver-*-open.html file may be different on your machine, so that path may need to be changed too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment