Skip to content

Instantly share code, notes, and snippets.

@lucaslugao
Last active November 27, 2017 12:41
Show Gist options
  • Save lucaslugao/e98ebf979c547ce4e4821f5e66e9f985 to your computer and use it in GitHub Desktop.
Save lucaslugao/e98ebf979c547ce4e4821f5e66e9f985 to your computer and use it in GitHub Desktop.
JupyerLab Launch Script
#!/bin/bash
original_path=$(pwd)
cd "$(dirname "$0")"
##PARAMETERS
port=5656
user=lucas.lugao-guimaraes
host=cuboide.polytechnique.fr
##CONFIG AUTODETECTION
hostname=$(hostname)
ip=$(/sbin/ifconfig | sed -n '/inet/p' | awk '{print $2}' | sed -n 1p | sed 's/addr://g')
if [ -z $(echo $hostname | awk '/polytechnique.fr/') ]; then
hostname=$ip
fi
lsof_paths=("/sbin/lsof" "/usr/bin/lsof")
for x in ${lsof_paths[@]}; do
if [ ! -z "$(which $x 2>/dev/null)" ]; then
lsof_path=$x
fi
done
link_handlers=("google-chrome" "google-chrome-stable" "google-chrome-beta" "/mnt/c/Windows/explorer.exe")
for x in ${link_handlers[@]}; do
if [ ! -z "$(which $x 2>/dev/null)" ]; then
link_handler=$x
fi
done
get_pids () {
pids=$(ps -u $USER -efww | awk '/bin\/jupyter-lab/' | awk '!/awk/' | awk '{print $2}')
}
get_current_port() {
current_port=$($lsof_path -Pa -p $pid -i | awk '/(LISTEN)/' | sed 's/.*\://' | grep -Po '[0-9]+')
if [ -z "$current_port" ]; then
current_port="$port"
fi
}
status() {
get_pids
if [[ -z "$pids" ]]; then
printf "No running jupyter-labs!\n"
return 1
else
for pid in $pids; do
get_current_port
printf "Jupyter-lab already running in port %s with pid %s.\n" "$current_port" "$pid"
printf "\n\t\thttp://%s:%s/\n\n" "$hostname" "$current_port"
done
fi
}
start (){
get_pids
if [[ -z "$pids" ]]; then
jupyter-lab --ip=$ip --port $port $original_path > /dev/null 2>&1 &
pid=$!
disown
sleep 1
printf "Jupyter-lab created in port %s with pid %s.\n" "$port" "$pid"
printf "\n\t\thttp://%s:%s/\n\n" "$hostname" "$port"
else
status
return 1
fi
return 0
}
stop (){
get_pids
if [[ -z "$pids" ]]; then
printf "No running jupyter-labs!\n"
return 1
else
for pid in $pids; do
kill -9 $pid && printf "Killed jupyter-lab with pid %s!\n" "$pid" || printf "Could not kill jupyter-lab with pid %s!\n" "$pid"
done
fi
return 0
}
restart() {
stop && sleep 1 && start
}
open() {
if [ -z "$link_handler" ]; then
printf "No link handler found!\n"
else
get_pids
if [[ -z "$pids" ]]; then
printf "No running jupyter-labs!\n"
return 1
else
pid=${pids[0]}
get_current_port
eval $link_handler "http://$hostname:$port/"
fi
fi
}
remote_open() {
if [ -z "$link_handler" ]; then
printf "No link handler found!\n"
else
printf "Remote answer from %s.%s:\n" "$user" "$host"
output=$(ssh $user@$host 'jl start')
printf "%s\n" "$output"
link=$(printf "$output" | awk '/http/' | awk '{ print $1;}')
if [ ! -z "$link" ]; then
eval $link_handler $link
fi
fi
}
remote_stop() {
printf "Remote answer from %s.%s:\n" "$user" "$host"
ssh $user@$host 'jl stop'
}
remote_status() {
printf "Remote answer from %s.%s:\n" "$user" "$host"
ssh $user@$host 'jl status'
}
print_usage_notice() {
printf "Usage: %s \n\t\tstart\n\t\tstop\n\t\trestart\n\t\tstatus\n\t\topen\n\t\tremote-open\n\t\tremote-stop\n\t\tremote-status\n" "$(basename "$0")"
}
update() {
printf "Updating script from gist.github!\n"
curl -H "Cache-Control: no-cache" https://gist.githubusercontent.com/lucaslugao/e98ebf979c547ce4e4821f5e66e9f985/raw/jl.sh > "$(basename "$0")"
}
if [ -z "$1" ]; then
print_usage_notice
fi
for cmd in "${@:1}"; do
case $cmd in
remote-open) remote_open ;;
remote-stop) remote_stop ;;
remote-status) remote_status ;;
start) start ;;
stop) stop ;;
restart) restart ;;
status) status ;;
open) open ;;
update) update ;;
*) print_usage_notice ;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment