Skip to content

Instantly share code, notes, and snippets.

@bayucandra
Created February 28, 2025 13:02
Show Gist options
  • Select an option

  • Save bayucandra/e7bae1a54e05b863f75e26a6be6b7e4e to your computer and use it in GitHub Desktop.

Select an option

Save bayucandra/e7bae1a54e05b863f75e26a6be6b7e4e to your computer and use it in GitHub Desktop.

Revisions

  1. bayucandra created this gist Feb 28, 2025.
    31 changes: 31 additions & 0 deletions gcp-workspace-mac.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/bin/bash

    # Config Vars
    MAX_IDLE_TIME_MINUTES=15
    GCP_COMPUTE_NAME=ml-workspace
    GCP_COMPUTE_ZONE=asia-southeast2-c
    GCP_COMPUTE_IS_STOPPED=0

    egress() {
    echo "Exiting script...."
    if [ $GCP_COMPUTE_IS_STOPPED -ne 1 ]; then
    gcloud compute instances stop ${GCP_COMPUTE_NAME} --zone ${GCP_COMPUTE_ZONE}
    fi
    }

    trap egress EXIT
    # trap egress SIGINT SIGTERM SIGTSTP
    gcloud compute instances start ${GCP_COMPUTE_NAME} --zone ${GCP_COMPUTE_ZONE}

    while true; do
    IDDLE_TIME_SECOND=$"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`"
    CUR_IDLE_TIME_MINUTES=$(($IDDLE_TIME_SECOND/60))
    echo -ne "Current idle time: ${IDDLE_TIME_SECOND} Seconds = ${CUR_IDLE_TIME_MINUTES} Minutes ........ "\\r
    if [ $CUR_IDLE_TIME_MINUTES -ge $MAX_IDLE_TIME_MINUTES ]; then
    echo "Stopping*****"
    gcloud compute instances stop ${GCP_COMPUTE_NAME} --zone ${GCP_COMPUTE_ZONE}
    GCP_COMPUTE_IS_STOPPED=1
    break
    fi
    sleep 1
    done