Last active
April 16, 2019 19:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace
<USERNAME>
,<SERVER>
and<userid>
.The
userid
and path to thenbserver-*-open.html
file may be different on your machine, so that path may need to be changed too.