Last active
March 18, 2023 00:21
-
-
Save SiteRelEnby/903436b3d21857a58de740ab9df7443a to your computer and use it in GitHub Desktop.
poweroff a docker buildx builder when it is idle (e.g. if it is a VM)
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
| #! /usr/bin/env bash | |
| [[ "${SLEEP_TIME}" == "" ]] && SLEEP_TIME=60 | |
| [[ "${SHUTDOWN_DELAY}" == "" ]] && SHUTDOWN_DELAY=1800 | |
| wait_for_builds(){ | |
| while (ps auxw | grep -E '[d]ocker +buildx +build') #[d] stops grep self-matching without wasting a process launch | |
| do | |
| echo "$(date --rfc-3339=seconds): builds are running" >&2 | |
| sleep ${SLEEP_TIME} | |
| done | |
| } | |
| wait_for_shutdown_delay(){ | |
| start_time=$(date +%s) | |
| until (ps auxw | grep -E '[d]ocker +buildx +build') | |
| do | |
| sleep ${SLEEP_TIME} | |
| if [[ "$(($(date +%s) - ${start_time}))" -gt "${SHUTDOWN_DELAY}" ]] | |
| then | |
| poweroff | |
| fi | |
| done | |
| } | |
| while true | |
| do | |
| wait_for_builds | |
| wait_for_shutdown_delay | |
| done #no need to escape since we poweroff when done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment