Created
February 28, 2025 13:02
-
-
Save bayucandra/e7bae1a54e05b863f75e26a6be6b7e4e to your computer and use it in GitHub Desktop.
Revisions
-
bayucandra created this gist
Feb 28, 2025 .There are no files selected for viewing
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 charactersOriginal 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