diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2022-07-15 19:13:35 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2022-07-15 19:13:35 -0400 |
commit | 683a45cf5d9841770bcf498726ea7b7bacbc7904 (patch) | |
tree | 48924c6488047d06aa9a0bd4d50b9eb855b4090a | |
parent | e2cb2364c9b5729a8662f6290f6e4d07d67781f5 (diff) | |
download | jupyter_safe_port-683a45cf5d9841770bcf498726ea7b7bacbc7904.tar.gz |
Make the scripted example work for humans
-rw-r--r-- | README.md | 52 |
1 files changed, 39 insertions, 13 deletions
@@ -84,12 +84,18 @@ Here are a few ways to use it... ```shell #!/usr/bin/env bash -if type -p jupyter_safe_port &>/dev/null; then +# System to spawn the notebook server on +server=example.lan + +# Conda environment to use +environ=XYZ + +if ! [[ -x $(command -v "jupyter_safe_port") ]]; then echo "jupyter_safe_port is not installed" >&2 exit 1 fi -result=$(jupyter_safe_port -d $server) +result="$(jupyter_safe_port -d $server)" if [ -z "$result" ]; then # jupyter_safe_port failed # case 1: not enough arguments @@ -100,21 +106,41 @@ if [ -z "$result" ]; then fi # Extract ports from result -read $port_local $port_remote <<< "$result" -if (( port_remote < 0 )); then { +read port_local port_remote <<< "$result" +if [[ -z "$port_local" ]]; then + # case 1: no local ports available + exit 1 +elif [[ -z "$port_remote" ]] || (( port_remote < 0 )); then # case 1: no remote ports available # case 2: if using '-c', no service is present on the requested port exit 1 -} +fi + +echo "Starting remote jupyter session on $server:$port_remote" +session="jupyter_${port_remote}" +ssh $server "tmux new-session -d -s $session \ + 'source ~/.bash_profile \ + && source ~/local/miniconda3/etc/profile.d/conda.sh \ + && conda activate $environ \ + && jupyter notebook --no-browser --port=$port_remote'" +if (( $? )); then + echo "Failed to connect to $server" >&2 + exit 1 +fi -# Spawn a local screen session to manage a remote jupyter server. -# You'd really want to use screen on the remote instead... This is just an example. -screen -dmS "jupyter_$server_$remote_port" ssh $server "source ~/miniconda3/etc/profile.d/conda.sh \ - && conda activate XYZ \ - && jupyter notebook --no-browser --port=$port_remote" +echo "Remote tmux session is: $session" +echo +echo "To kill the notebook server:" +echo "ssh $server 'tmux kill-session -t $session'" +echo # Forward the ports to the local system -echo "Forwarding $port_remote to localhost:$port_local" -echo "To interrupt the session press: ctrl-c -ssh -N $port_local:localhost:$port_remote $server +echo "Forwarding $server:$port_remote to localhost:$port_local" +echo "To interrupt the session press: ctrl-c" + +ssh -N -L$port_local:localhost:$port_remote $server +if (( $? )); then + echo "Failed to forward port." >&2 + exit 1 +fi ``` |