aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2022-07-16 01:21:36 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2022-07-16 01:21:36 -0400
commite1db7660b801696d54e8ad398ede3ae95087ea28 (patch)
treeecadd9b5b3c319884e2214d1218b008bb8a6d206
parent5af1b4caa045b6a06f73e5de2be93488c99d240f (diff)
downloadjupyter_safe_port-e1db7660b801696d54e8ad398ede3ae95087ea28.tar.gz
Clean up example scriptHEADmaster
-rw-r--r--README.md44
1 files changed, 32 insertions, 12 deletions
diff --git a/README.md b/README.md
index 2b72e8a..a808082 100644
--- a/README.md
+++ b/README.md
@@ -83,20 +83,32 @@ Here are a few ways to use it...
```shell
#!/usr/bin/env bash
+interrupt() {
+ echo "Interrupted" >&2
+ exit 0
+}
+trap interrupt SIGINT SIGTERM
# System to spawn the notebook server on
server=example.lan
-# Conda environment to use
+# Where is conda located? (Use single-quotes to avoid expanding ~ or variables)
+conda_root='~/miniconda3'
+
+# Conda environment to activate
environ=XYZ
+# Configure port range (optional)
+range_low=
+range_high=
+
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)"
-if [ -z "$result" ]; then
+if (( $? )) || [ -z "$result" ]; then
# jupyter_safe_port failed
# case 1: not enough arguments
# case 2: invalid argument
@@ -107,30 +119,38 @@ fi
# Extract ports from result
read port_local port_remote <<< "$result"
-if [[ -z "$port_local" ]]; then
- # case 1: no local ports available
+
+if [[ -z "$port_local" ]] || [[ -z "$port_remote" ]]; then
+ # case 1: unhandled exception
+ exit 1
+fi
+
+if (( port_local < 0 )); then
+ # case 1: no local ports are available in range
exit 1
-elif [[ -z "$port_remote" ]] || (( port_remote < 0 )); then
- # case 1: no remote ports available
+fi
+
+if (( port_remote < 0 )); then
+ # case 1: no remote ports are available in range
# 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 \
+ssh $server "bash -c 'tmux new-session -d -s $session \
+ \"source ~/.bash_profile \
+ && source $conda_root/etc/profile.d/conda.sh \
&& conda activate $environ \
- && jupyter notebook --no-browser --port=$port_remote'"
+ && jupyter notebook --no-browser --port=$port_remote\"'"
if (( $? )); then
echo "Failed to connect to $server" >&2
exit 1
fi
-echo "Remote tmux session is: $session"
+echo "Remote tmux session name: $session"
echo
-echo "To kill the notebook server:"
+echo "To kill the tmux session and the jupyter notebook server:"
echo "ssh $server 'tmux kill-session -t $session'"
echo