-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjup
More file actions
executable file
·107 lines (95 loc) · 3.9 KB
/
jup
File metadata and controls
executable file
·107 lines (95 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
###############################################################################
#
# Start or stop Jupyter Notebook server in the background.
# Start if not running, stop if running, or use argument start/stop.
# Use https via certificate and key in ~/.jupyter
# Bind to first public IPv4 address for access from the LAN.
# Do not open browser.
# Redirect console output to ~/.jupyter/run.log
# Save process ID in ~/.jupyter/pid.log
#
# Run once before use:
# jupyter-notebook --generate-config
# jupyter-notebook password
# openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
# -keyout ~/.jupyter/mykey.key -out ~/.jupyter/mycert.pem
#
# Exit status:
# 0: No error
# 1: Cannot access ~/.jupyter
# 2: Argument stop but server not running
# 3: Argument start but server already running
# 4: Failed to stop server
# 5: Unable to remove ~/.jupyter/pid.log
# 6: Certificate not found ~/.jupyter/mycert.pem
# 7: Key not found ~/.jupyter/mykey.key
# 8: Cannot write to ~/.jupyter/run.log
# 9: Cannot write to ~/.jupyter/pid.log
# 10: Unknown operating system
# 11: App not found
# 12: No IPv4 address found
# 13: Invalid IPv4 address
#
# Version : 1.3
# Date : 2023-05-08
# Author : E. Dronkert https://github.com/ednl
# License : MIT (free to use in any way; attribution to author appreciated)
#
###############################################################################
APP=jupyter-notebook
PORT=8888
DIR=~/.jupyter
CER="$DIR/mycert.pem"
KEY="$DIR/mykey.key"
LOG="$DIR/run.log"
PID="$DIR/pid.log"
HOST=$(uname -n) # host name (probably) bound to LAN IP address
SYST=$(uname -s) # Linux or Darwin (=MacOS)
# Directory must exist
[ -e "$DIR" -a -d "$DIR" -a -r "$DIR" -a -w "$DIR" -a -x "$DIR" ] || { echo "Cannot access: $DIR" >&2; exit 1; }
# Is there a PID file and is the PID valid?
RUN=$(pgrep -F "$PID" 2>/dev/null)
# If no script argument: set to start if no PID, set to stop if PID found
[ -z "$1" -a -z "$RUN" ] && set -- start
[ -z "$1" -a -n "$RUN" ] && set -- stop
# Check if argument makes sense
[ "$1" = "stop" -a -z "$RUN" ] && { echo "Jupyter Notebook server not running" >&2; exit 2; }
[ "$1" = "start" -a -n "$RUN" ] && { echo "Jupyter Notebook server already running" >&2; exit 3; }
# Stop the Jupyter Notebook server
if [ "$1" = "stop" ]; then
echo -n "Stopping Jupyter Notebook server ... "
pkill -F "$PID" || { echo "Failed"; exit 4; }
echo "OK"
rm -f "$PID" || { echo "Unable to remove: $PID" >&2; exit 5; }
exit 0
fi
# Start the Jupyter Notebook server
if [ "$1" = "start" ]; then
[ -e "$CER" -a -f "$CER" -a -r "$CER" -a -s "$CER" ] || { echo "Certificate not found: $CER" >&2; exit 6; }
[ -e "$KEY" -a -f "$KEY" -a -r "$KEY" -a -s "$KEY" ] || { echo "Key not found: $KEY" >&2; exit 7; }
[ ! -e "$LOG" -o -w "$LOG" ] || { echo "Cannot write to log file: $LOG" >&2; exit 8; }
[ ! -e "$PID" -o -w "$PID" ] || { echo "Cannot write to PID file: $PID" >&2; exit 9; }
if [ "$SYST" = "Linux" ]; then
IP=$(ip -4 -br a | grep UP | head -n 1 | awk '{print $3}' | cut -d'/' -f1)
elif [ "$SYST" = "Darwin" ]; then
INTF=$(ifconfig | grep '^en.*<UP' | grep 'RUNNING' | sort | head -n 1 | cut -d':' -f1)
IP=$(ifconfig "$INTF" inet | tail -n 1 | awk '{print $2}')
else
echo "Unknown operating system: $SYST" >&2
exit 10
fi
# Check if app exists
[ -n "$(which ${APP})" ] || { echo "App not found: $APP" >&2; exit 11; }
[ -z "$IP" ] && { echo "No IPv4 address found" >&2; exit 12; }
echo "$IP" | grep -q -s -E '^[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}$' || { echo "Invalid IPv4 address: $IP" >&2; exit 13; }
echo -n "Starting Jupyter Notebook server ... "
#$APP > "$LOG" 2>&1 &
$APP --ip "$IP" --port "$PORT" --no-browser --certfile "$CER" --keyfile "$KEY" > "$LOG" 2>&1 &
echo $! > "$PID"
echo "OK"
# Lowercase host name
echo "https://${HOST,,}:$PORT"
echo "https://$IP:$PORT"
exit 0
fi