Created
July 25, 2024 08:21
-
-
Save maartenterpstra/067a2f4d9bb3a495ddec0b11b388d024 to your computer and use it in GitHub Desktop.
Automatic Jupyter setup with kernel culling
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 | |
if [[ "$1" == "-h" ]]; then | |
echo "**Usage:** ./setup_jupyter.sh <environment> <port>" | |
echo "" | |
echo " environment (required): The name of the conda environment to create. Use base to only provide the configuration and conda is already set-up." | |
echo " port (required): The port number on which to run the Jupyter server (integer >= 5000)." | |
exit 0 | |
fi | |
# Check for at least two arguments (environment and port) | |
if [[ $# -lt 2 ]]; then | |
echo "Error: Please provide both environment name (optional) and port number." | |
echo "Run './setup_jupyter.sh -h' for help." | |
exit 1 | |
fi | |
# Check if port number is an integer greater than or equal to 5000 | |
if [[ ! "$2" =~ ^[0-9]+$ ]] || [[ "$2" -lt 5000 ]]; then | |
echo "Error: Port number must be an integer greater than or equal to 5000." | |
echo "Run './setup_jupyter.sh -h' for help." | |
exit 1 | |
fi | |
if command -v conda >/dev/null 2>&1; then | |
condapath=$(which conda) | |
echo "Conda is found in $condapath" | |
else | |
echo "Conda is not found. Downloading..." | |
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh | |
chmod +x Miniconda3-latest-Linux-x86_64.sh | |
./Miniconda3-latest-Linux-x86_64.sh | |
rm Miniconda3-latest-Linux-x86_64.sh | |
source "$HOME/.bashrc" | |
fi | |
if ! command -v conda &> /dev/null; then | |
echo "conda is not available in the path. Exiting." | |
exit 1 | |
fi | |
conda_env_name=$1 | |
config_file="$HOME/.jupyter/jupyter_server_config.py" | |
if [ -f "$config_file" ]; then | |
exists=1 | |
else | |
exists=0 | |
fi | |
if conda env list | grep -q "^$conda_env_name"; then | |
echo "Environment '$conda_env_name' already exists. Not creating another one" | |
else | |
echo "Creating conda environment $conda_env_name..." | |
conda create --name "$conda_env_name" python=3.12 | |
fi | |
#activate the environment | |
#source activate "$conda_env_name" | |
eval "$(conda shell.bash hook)" | |
conda activate "$conda_env_name" | |
#echo "Done! Installing pytorch torchvision torchaudio matplotlib numpy jupyter notebook" | |
conda install pytorch torchvision torchaudio matplotlib numpy jupyter notebook pytorch-cuda=12.1 -c pytorch -c nvidia | |
#conda install jupyter notebook | |
echo "Done! Generating jupyter configuration" | |
jupyter server --generate-config | |
if [ -f "$HOME/.jupyter/jupyter_server_config.json" ] && grep "password" "$HOME/.jupyter/jupyter_server_config.json" >/dev/null 2>&1; then | |
echo "It seems you already have a Jupyter password. Skipping..." | |
else | |
echo "Pick a password" | |
jupyter notebook password | |
fi | |
edit_file_with_pattern() { | |
# Function arguments | |
local file_path="$1" | |
local search_pattern="$2" | |
local new_line="$3" | |
# Check if required arguments are provided | |
if [[ -z "$file_path" || -z "$search_pattern" || -z "$new_line" ]]; then | |
echo "Error: Please provide all required arguments: file_path, search_pattern, new_line" | |
return 1 | |
fi | |
# Use sed to edit the file in-place | |
sed -i "/$search_pattern/c\\$new_line" "$file_path" | |
} | |
echo "Done! Setting Jupyter notebook defaults" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.ServerApp\.port\s*=\s*" "c.ServerApp.port = $2" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.ServerApp\.ip\s*=\s*" "c.ServerApp.ip = '*'" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.ServerApp\.open_browser\s*=\s*" "c.ServerApp.open_browser = False" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.ServerApp\.allow_remote_access\s*=\s*" "c.ServerApp.allow_remote_access = True" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.ServerApp\.shutdown_no_activity_timeout\s*=\s*" "c.ServerApp.shutdown_no_activity_timeout = 3*24*60*60" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.MappingKernelManager\.cull_connected\s*=\s*" "c.MappingKernelManager.cull_connected = True" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.MappingKernelManager\.cull_idle_timeout\s*=\s*" "c.MappingKernelManager.cull_idle_timeout = 24*60*60" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.AsyncMappingKernelManager\.cull_connected\s*=\s*" "c.AsyncMappingKernelManager.cull_connected = True" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.AsyncMappingKernelManager\.cull_idle_timeout\s*=\s*" "c.AsyncMappingKernelManager.cull_idle_timeout = 24*60*60" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.GatewayMappingKernelManager\.cull_connected\s*=\s*" "c.GatewayMappingKernelManager.cull_connected = True" | |
edit_file_with_pattern "$config_file" "^\s*#\?\s*c\.GatewayMappingKernelManager\.cull_idle_timeout\s*=\s*" "c.GatewayMappingKernelManager.cull_idle_timeout = 24*60*60" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment