Skip to content

Instantly share code, notes, and snippets.

@kiwimato
Created November 17, 2021 18:11
Show Gist options
  • Save kiwimato/275412ecc1fa44316c5a25b37da8ad08 to your computer and use it in GitHub Desktop.
Save kiwimato/275412ecc1fa44316c5a25b37da8ad08 to your computer and use it in GitHub Desktop.
Azure DevOps: stop the agent by issuing a shutdown if the agent is idle and it ran at least one job. If a job is running, it waits for it to finish. Useful with EC2 spot based instances as agents.
# Create a service:
cat > /lib/systemd/system/azure-devops-shutdown-when-idle.service<<'EOF'
[Unit]
Description=Issues as shutdown in case the agent is idle, to avoid long running ec2 spot instances
[Service]
Type=simple
ExecStart=/bin/bash /root/shutdown_when_idle.sh
[Install]
WantedBy=multi-user.target
EOF
cat >/root/shutdown_when_idle.sh<<'EOF'
# Azure DevOps: shutdown agent when idle
# This script will continuously check if there's a job running, and if there's nothing, then it will issue a shutdown
# It's mainly designed to be used together with EC2 spot instances, since we're using them just once / build.
# Check if the agent ran at least one job, if not wait max $max_minutes
max_minutes=5
start_time=$(date +%s)
max_seconds=$(( $max_minutes * 60 )) # minutes * seconds
while [[ "$(($(date +%s) - $start_time))" -lt "$max_seconds" ]];do
if [[ "$(ls /azp/agent/_diag | grep Worker_* | wc -l)" -eq "0" ]];then
echo "The agent did not run any job so far, waiting max $max_minutes minutes"
else
echo "Job log file found"
break
fi
sleep 1
done
# Number of processes of azure devops in idle (2).
IDLE_PROC_COUNT=2
# If the load is higher than $IDLE_PROC_COUNT, it means there's a job running, otherwise issue a shutdown.
while :; do
if [[ "$(ps --forest --no-headers -g `systemctl show --property MainPID --value azure-devops-agent` | wc -l )" -gt ${IDLE_PROC_COUNT} ]]; then
echo A job is currently running
sleep 1s
else
echo Job seems to have finished. Issuing a shutdown
shutdown -h now
fi
sleep 1
done
EOF
systemctl daemon-reload
systemctl enable azure-devops-shutdown-when-idle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment