Skip to content

Instantly share code, notes, and snippets.

@soxofaan
Last active March 23, 2021 23:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soxofaan/693e999877f15068fec7ee015a1fc48f to your computer and use it in GitHub Desktop.
Save soxofaan/693e999877f15068fec7ee015a1fc48f to your computer and use it in GitHub Desktop.
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