Skip to content

Instantly share code, notes, and snippets.

@bayucandra
Created February 28, 2025 13:02
Show Gist options
  • Save bayucandra/e7bae1a54e05b863f75e26a6be6b7e4e to your computer and use it in GitHub Desktop.
Save bayucandra/e7bae1a54e05b863f75e26a6be6b7e4e to your computer and use it in GitHub Desktop.
gcp-workspace-mac
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment