#!/bin/bash
#SBATCH --job-name=start-jupyter
#SBATCH --partition=normal_q 
#SBATCH --nodes=1 #number of nodes
#SBATCH --ntasks-per-node=16 #maximum number of tasks per node
#SBATCH --mem=16G #memory per node
#SBATCH --time=00:10:00 #specify the runtime using HH:MM:SS 
#SBATCH --account= slurm_account #replace it with your slurm account name
#SBATCH --mail-user PID@vt.edu #replace it with your own email address
#SBATCH --mail-type BEGIN
#SBATCH --mail-type END
#SBATCH --output=start-jupyter.%J.out
#SBATCH --error=start-jupyter.%J.err

# Create an ssh tunnel
XDG_RUNTIME_DIR=""
node=$(hostname -s)
user=$(whoami)
cluster="tinkercliffs1"
port=8888 # you can replace 8888 with 8889, 8890, 8891, or 8892.

ssh -N -f -R ${port}:localhost:${port} ${user}@${cluster}.arc.vt.edu

# Print the instructions 
echo -e "
# Note: default port: 8888. In case port 8888 is not available, please go to start-jupyter.%J.err to find the alternative port info. Or simply replace 8888 with 8889, 8890, 8891, or 8892 in the above code line.

# Run this command in the terminal on your laptop:
ssh -N -f -L 8888:${node}:8888 ${user}@${cluster}.arc.vt.edu

# Use a browser on your local machine to go to:
http://localhost:8888/
"

# Load the module and activate the conda environment 
module load Anaconda3/2024.02-1
source activate test #replace "test" with the conda environment you intend to work in

# If jupyter is not installed, please remove the “#” in the following line:
# conda install jupyter -y

# Run Jupyter
echo "starting jupyter notebook"
jupyter-notebook --no-browser --port=${port} --ip=${node}

#   Keep the interactive session alive for x seconds: 600 seconds=10 minutes
sleep 600

exit
