Skip to content

Instantly share code, notes, and snippets.

@soxofaan

soxofaan/here-jupyter.sh

Last active Aug 27, 2019
Embed
What would you like to do?
here-jupyter: launch Jupyter Notebook in background from current directory using tmux
#!/bin/bash
# Build tmux session name from current directory
SESSION_NAME=$(pwd)
# Strip non-alphanumeric characters
SESSION_NAME=${SESSION_NAME//[^a-zA-Z0-9]/}
# Only keep trailing part
SESSION_NAME=Jupyter-${SESSION_NAME: -20}
# Command to run inside tmux session
CONDA_ENV=jupyter
# Anaconda environment activation is based on https://github.com/conda/conda/issues/7980
COMMAND='eval "$(conda shell.bash hook)";'
COMMAND+="conda activate $CONDA_ENV;"
COMMAND+='jupyter notebook;'
COMMAND+='sleep 2;'
echo "Starting tmux session '$SESSION_NAME' in background with command $COMMAND"
if tmux new-session -s $SESSION_NAME -d "$COMMAND"; then
echo "Tmux session launched with initial output:"
sleep 1
tmux capture-pane -p -t $SESSION_NAME
echo "To re-attach:"
echo " tmux attach -t $SESSION_NAME"
else
echo "Failed to launch tmux"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment