Skip to content

Instantly share code, notes, and snippets.

@yoshihikosuzuki
Last active February 11, 2020 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshihikosuzuki/a0b89404063368ddaabc30c8173d2a7d to your computer and use it in GitHub Desktop.
Save yoshihikosuzuki/a0b89404063368ddaabc30c8173d2a7d to your computer and use it in GitHub Desktop.
Command (and automator application in Mac) for remote Jupyter Notebook execution from local
HOST_NAME=
PORT_ID_LOCAL=
PORT_ID_REMOTE=
# Specify "direct" or "sge"
EXEC_TYPE="sge"
# Commands for checking existing jupyter process and for running jupyter
# Put `run_jupyter.sh` at the home directory in the remote environment if using a job scheduler
if [ ${EXEC_TYPE} = "direct" ]; then
CHECK_JUPYTER="ps x"
RUN_JUPYTER="nohup jupyter notebook >/dev/null 2>&1 &"
elif [ ${EXEC_TYPE} = "sge" ]; then
CHECK_JUPYTER="qstat"
# Options other than those specified here should be given in the remote file
RUN_JUPYTER="qsub -N jupyter_nb -pe smp 4 -l hostname=${HOST_NAME} run_jupyter.sh"
else
echo "Invalid value of `EXEC_TYPE`: ${EXEC_TYPE}"
exit 1
fi
# Open the port
lsof -s TCP:LISTEN -i :${PORT_ID_LOCAL} | awk 'NR > 1 {print $2}' | uniq | while read PID; do kill -KILL ${PID}; done
# Run jupyter if not existing
N_JUPYTER_PROC=$(ssh ${HOST_NAME} "${CHECK_JUPYTER}" | grep "jupyter" | wc -l)
if [ ${N_JUPYTER_PROC} = 0 ]; then
ssh ${HOST_NAME} "source ~/.bash_profile; ${RUN_JUPYTER}"
sleep 10
fi
# Start connection and then open a chrome tab
ssh -N -f \
-L localhost:${PORT_ID_LOCAL}:localhost:${PORT_ID_REMOTE} \
-o PermitLocalCommand=yes \
-o LocalCommand="open -a '/Applications/Google Chrome.app' http://localhost:${PORT_ID_LOCAL}" \
${HOST_NAME} \
&
# [Optional for macOS] Command for automator (assuming you have this script file at the home directory)
#nohup $HOME/remote_jupyter.sh > /dev/null 2>&1 &
## Put this script at your home directory of the remote environment.
#!/bin/bash
#$ -o sge.log
#$ -j y
#$ -S /bin/bash
#$ -cwd
#$ -V
#$ -q all.q
jupyter notebook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment