Last active
March 23, 2021 23:34
-
-
Save soxofaan/693e999877f15068fec7ee015a1fc48f to your computer and use it in GitHub Desktop.
here-jupyter: launch Jupyter Notebook in background from current directory using tmux
This file contains hidden or 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
#!/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