A repository of bitesize articles, tips & tricks
(in both English and French) curated by Mirego’s team.

Until I can SSH…

I was working on a virtual machine setup to run GitHub Actions self-hosted runners. I’m launching a VM and I wanted to connect to it via SSH when it’s ready to accept SSH connections.

I never had to use Bash’s until until (!) today but it seemed like a good use case for this. Try to connect to the VM (with a 1-second timeout), keep trying until it works and when it does… let’s go!

I ended up with this snippet:

echo "💤 [HOST] Waiting for SSH to be available on VM"
until [ "$(ssh -q -o ConnectTimeout=1 $VM_USERNAME@$VM_HOSTNAME pwd)" ]
do
  echo "💤 [HOST] Still waiting for SSH…"
done

echo "🏃 [HOST] Start runner on VM"
ssh $VM_USERNAME@$VM_HOSTNAME "./some-redacted-script-here.sh"

I think it’s pretty cool 😎