Skip to content

Instantly share code, notes, and snippets.

@SiteRelEnby
Last active March 18, 2023 00:21
Show Gist options
  • Select an option

  • Save SiteRelEnby/903436b3d21857a58de740ab9df7443a to your computer and use it in GitHub Desktop.

Select an option

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)
#! /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