Skip to content

Instantly share code, notes, and snippets.

@ntraft
Last active January 31, 2024 05:48
Show Gist options
  • Save ntraft/7e35a293a2ca3625f366cd3683c4c83a to your computer and use it in GitHub Desktop.
Save ntraft/7e35a293a2ca3625f366cd3683c4c83a to your computer and use it in GitHub Desktop.
A pair of scripts which can be used to launch Jupyter notebooks on the Vermont Advanced Computing Center
#!/bin/bash
# Specify a partition
#SBATCH --partition=bdgpu
# Request nodes
#SBATCH --nodes=1
# Request some processor cores
#SBATCH --ntasks=8
# Request a GPU
#SBATCH --gpus=0
# Request memory
#SBATCH --mem=40G
# Maximum runtime in hours
#SBATCH --time=48:00:00
# Name of this job
#SBATCH --job-name=jupyter
# Allow for the use of conda activate
source ~/.bash_profile
# Move to submission directory
cd ${SLURM_SUBMIT_DIR}
# your job execution follows:
whoami
hostname
conda activate deep-amd-3.9
time jupyter notebook --no-browser --port=8900
#!/bin/bash
out_dir=$DEVDIR/tmp
mkdir -p $out_dir
jup_outfile=$out_dir/jupyter.out
# First, kill ssh forwards from previous times running this command which may be left hanging
# around without getting properly cleaned up.
# WARNING: This assumes we're only running one notebook at a time!
if [ -f $jup_outfile ]; then
found=false
port=$(grep "http://localhost" $jup_outfile | sed -E 's/.*localhost:([0-9]+).*/\1/' | head -1)
while read usr pid rest; do
if [ ! -z $pid ]; then
echo "Killing previous connection: $usr $pid $rest"
kill -9 $pid
found=true
fi
done <<< "$(ps -ef | grep $USER'.*[0-9] ssh.*'$port)"
if $found; then echo ""; fi
fi
>$jup_outfile # clear the output file
# start the jupyter server on the cluster
sbatch --output=$jup_outfile $DEVDIR/jupyter-server.sbatch
echo "Jupyter notebook server starting on compute node, waiting..."
# wait for notebook server to start
while ! grep -q "http://localhost" $jup_outfile; do
sleep 1
done
# get port
port=$(grep "http://localhost" $jup_outfile | sed -E 's/.*localhost:([0-9]+).*/\1/' | head -1)
# get token
token=$(grep "http://localhost:$port/?token=" $jup_outfile | sed 's/.*token=\(.*\)/\1/'| head -1)
# get user
user=$(head -n 1 $jup_outfile | sed 's/.*\[\(.*\)\].*/\1/' | head -1)
# get host
host=$(head -n 2 $jup_outfile | tail -n 1 | sed 's/.*\[\(.*\)\].*/\1/' | head -1)
echo "Jupyter notebook server started on compute node $user@$host"
sleep 1
# connect to notebook server
ssh -fNT -L $port:localhost:$port $user@$host &
echo "Port forwarding started $user@$host:$port -> localhost:$port"
echo ""
echo "connect to notebook server at:"
echo " http://localhost:$port/?token=$token"
echo ""
@ntraft
Copy link
Author

ntraft commented Jan 31, 2024

When you use these scripts, be sure to:

  • Edit the start script to configure the output location.
  • Edit the sbatch script to configure your resource request and conda env.
  • Remember that you need to change the resources and environment as needed whenever you run.
  • Note that the port number is (unfortunately) hard-coded, and you must use this port number when connecting to the notebook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment