Last active
January 21, 2025 02:46
-
-
Save bayucandra/fd99b7f08db02e3a6a0aceabe314c1ff to your computer and use it in GitHub Desktop.
GCP compute instance auto shutdown when vscode remote edit currently doesn't run/connected
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 | |
# Requred packages to install: | |
# sudo, lsof, gcloud, | |
# CONFIG VARS=================== | |
LAST_RUN_CHECK_FILE_NAME="./last-run-check" #Place to store date time when last port check performed | |
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | |
RUNNING_PROCESS_NAME=code-fabd | |
MAX_LAST_RUN_MINUTES=20 | |
GCP_COMPUTE_INSTANCE_NAME="ml-workspace" | |
GCP_COMPUTE_ZONE=asia-southeast2-c | |
cd $SCRIPT_DIR; | |
if [ "$1" = "clear" ]; then | |
echo "Clearing config..." | |
cp $LAST_RUN_CHECK_FILE_NAME "${LAST_RUN_CHECK_FILE_NAME}.old" | |
rm $LAST_RUN_CHECK_FILE_NAME | |
echo "$(date): $LAST_RUN_CHECK_FILE_NAME has cleared" > "clear.log" | |
exit 0 | |
fi | |
update_last_run_time() { | |
echo $(date +%s) > $LAST_RUN_CHECK_FILE_NAME | |
} | |
get_last_run_time() { | |
RET=$(cat ${LAST_RUN_CHECK_FILE_NAME}) | |
if [ -z "${RET}" ]; then update_last_run_time; RET=$(date +%s); fi | |
echo $RET | |
} | |
get_last_run_diff() { | |
LAST_RUN_TIME=$(get_last_run_time) | |
CUR_TIME=$(date +%s) | |
RET=$(($CUR_TIME-$LAST_RUN_TIME)) | |
echo $RET | |
} | |
PROCESS_RUNNING_COUNT_SCRIPT="sudo lsof -i -P -n | grep ESTABLISHED | grep $RUNNING_PROCESS_NAME | wc -l" | |
echo "PROCESS_RUNNING_COUNT_SCRIPT = ${PROCESS_RUNNING_COUNT_SCRIPT}" | |
PROCESS_RUNNING_COUNT="$(sudo lsof -i -P -n | grep ESTABLISHED | grep $RUNNING_PROCESS_NAME | wc -l)" | |
echo "PROCESS_RUNNING_COUNT = ${PROCESS_RUNNING_COUNT}" | |
if [ $PROCESS_RUNNING_COUNT -ge 1 ]; then update_last_run_time; fi | |
LAST_RUN_SECONDS=$(get_last_run_diff) | |
LAST_RUN_MINUTES=$(($LAST_RUN_SECONDS/60)) | |
echo "$(date): Last run in minutes: $LAST_RUN_MINUTES" | |
if [ $LAST_RUN_MINUTES -ge $MAX_LAST_RUN_MINUTES ]; then | |
echo "Shuttingdown instance===============" | |
rm $LAST_RUN_CHECK_FILE_NAME | |
cp run.log run.log.last | |
gcloud compute instances stop ${GCP_COMPUTE_INSTANCE_NAME} --zone ${GCP_COMPUTE_ZONE} | |
echo "stopped*********" | |
fi | |
# crontab | |
#================= | |
# * * * * * /home/user/gcp/vm.sh > /home/user/gcp/run.log | |
# File name = gcp-auto-stop.shutdown.service | |
# command sudo systemctl enable gcp-auto-stop.shutdown | |
# =========================================== | |
# [Unit] | |
# Description=Clear GCP auto stop file state on shutdown | |
# [Service] | |
# Type=oneshot | |
# RemainAfterExit=true | |
# ExecStart=/home/user/gcp/vm.sh clear | |
# ExecStop=/home/user/gcp/vm.sh clear | |
# [Install] | |
# WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment