Skip to content

Instantly share code, notes, and snippets.

@jorishr
Last active December 29, 2023 20:47
Show Gist options
  • Save jorishr/49c2062c97707daaf856d09fcf4b8d21 to your computer and use it in GitHub Desktop.
Save jorishr/49c2062c97707daaf856d09fcf4b8d21 to your computer and use it in GitHub Desktop.

Revisions

  1. jorishr revised this gist Dec 29, 2023. 1 changed file with 51 additions and 16 deletions.
    67 changes: 51 additions & 16 deletions setup_temp_swap.sh
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,67 @@
    #!/bin/bash
    # NOTE: use a separate tmux session to run this script as it is set to and occopy the terminal session for 15 min

    set -e
    echo -e "\e[1;34m╔══════════════════════════════════════════╗"
    echo -e "║ \e[1;36mSwap File Setup Script\e[1;34m ║"
    echo -e "╚══════════════════════════════════════════╝\e[0m\n"

    # Step 1: Setup the Swap File
    echo -e "\e[1;34mStep 1: Setup the Swap File\e[0m"
    echo -e "Creating swap file..."

    SWAP_FILE="/swapfile"
    MAX_MINUTES=120

    # Prompt user for input if no argument is provided
    if [ "$#" -eq 0 ]; then
    read -p "Enter the duration in minutes (max. $MAX_MINUTES minutes): " USER_INPUT_MINUTES

    # Validate user input
    if ! [[ "$USER_INPUT_MINUTES" =~ ^[0-9]+$ ]] || [ "$USER_INPUT_MINUTES" -gt "$MAX_MINUTES" ]; then
    echo "Invalid input. Please enter a valid number less than or equal to $MAX_MINUTES."
    exit 1
    fi

    DURATION="$USER_INPUT_MINUTES"
    else
    # Validate command line argument
    if ! [[ "$1" =~ ^[0-9]+$ ]] || [ "$1" -gt "$MAX_MINUTES" ]; then
    echo "Invalid argument. Please enter a valid number less than or equal to $MAX_MINUTES."
    exit 1
    fi

    DURATION="$1"
    fi


    SWAP_SIZE="8G"

    echo "Creating swap file..."
    sudo fallocate -l $SWAP_SIZE $SWAP_FILE
    sudo chmod 600 $SWAP_FILE
    sudo mkswap $SWAP_FILE

    # Step 2: Activate the Swap File
    echo -e "\n\e[1;34mStep 2: Activate the Swap File\e[0m"
    echo -e "Activating swap file..."

    echo "Activating swap file..."
    sudo swapon $SWAP_FILE

    # Step 3: Wait for 15min
    echo -e "\e[1;34mStep 3: Wait for $DURATION minutes in the background\e[0m"
    echo -e "Waiting for $DURATION minutes..."

    {
    (sleep 15m && {

    echo -e "\e[1;34mStep 4: Remove the Swap File\e[0m"
    echo -e "\e[1;36m[$(date '+%Y-%m-%d %H:%M:%S')] Removing swap file...\e[0m"

    sudo swapoff $SWAP_FILE
    sudo rm $SWAP_FILE

    echo -e "\e[1;32m[$(date '+%Y-%m-%d %H:%M:%S')] Swap file removed.\e[0m"

    echo "Waiting for 15min in background..."
    (sleep 15m {
    } & disown )
    } >> /home/jorishr/.setup-swapfile.log 2>&1 &

    # Step 4: Remove the Swap File
    echo "Removing swap file..."
    sudo swapoff $SWAP_FILE
    sudo rm $SWAP_FILE
    echo "Swap file removed."
    }) &
    echo "Background process start. Terminal now free to use."
    echo -e "Background process started. The terminal is now free."
    echo -e "Swapfile should be removed in $DURATION minutes. Result in log file: .setup-swapfile.log in home folder.\n"
    echo -e "\e[1;34m╔══════════════════════════════════════════╗"
    echo -e "║ \e[1;36mSwap File Setup Script Completed\e[1;34m ║"
    echo -e "╚══════════════════════════════════════════╝\e[0m"
  2. jorishr revised this gist Dec 29, 2023. 1 changed file with 10 additions and 9 deletions.
    19 changes: 10 additions & 9 deletions setup_temp_swap.sh
    Original file line number Diff line number Diff line change
    @@ -20,12 +20,13 @@ sudo swapon $SWAP_FILE

    # Step 3: Wait for 15min

    echo "Waiting for 15min..."
    sleep 15m

    # Step 4: Remove the Swap File
    echo "Removing swap file..."
    sudo swapoff $SWAP_FILE
    sudo rm $SWAP_FILE

    echo "Swap file removed."
    echo "Waiting for 15min in background..."
    (sleep 15m {

    # Step 4: Remove the Swap File
    echo "Removing swap file..."
    sudo swapoff $SWAP_FILE
    sudo rm $SWAP_FILE
    echo "Swap file removed."
    }) &
    echo "Background process start. Terminal now free to use."
  3. jorishr created this gist Dec 15, 2023.
    31 changes: 31 additions & 0 deletions setup_temp_swap.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/bin/bash
    # NOTE: use a separate tmux session to run this script as it is set to and occopy the terminal session for 15 min

    set -e

    # Step 1: Setup the Swap File

    SWAP_FILE="/swapfile"
    SWAP_SIZE="8G"

    echo "Creating swap file..."
    sudo fallocate -l $SWAP_SIZE $SWAP_FILE
    sudo chmod 600 $SWAP_FILE
    sudo mkswap $SWAP_FILE

    # Step 2: Activate the Swap File

    echo "Activating swap file..."
    sudo swapon $SWAP_FILE

    # Step 3: Wait for 15min

    echo "Waiting for 15min..."
    sleep 15m

    # Step 4: Remove the Swap File
    echo "Removing swap file..."
    sudo swapoff $SWAP_FILE
    sudo rm $SWAP_FILE

    echo "Swap file removed."