Last active
February 11, 2020 08:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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