We patch and reboot a large number of Windows instances via AWS SSM commands currently but there is an issue where the SSM agent doesn't always check in. My thought process was to speed things up I can run a while loop on each of our patch groups that pulls the uptime of each instance in the group. Once all instances had been up for say 2 minutes the next group will be rebooted. I can't seem to get past a simple issue with how to properly create a while loop to accomplish this though.
When running the below commands as a bash script I'm getting an error that mentions I'm trying to use an "invalid arithmetic operator" in the loop. I have tested by outputting the value of $upTime and it was '18'.
This seems like one of those small mess ups that I am just wasting way too much time on and someone will be able to quickly point out what I am doing wrong. If the output of $upTime is a number I assumed the less than in the loop would function. If I just hard set it to 18 it loops properly which makes me think it must be some weird formatting issue from the SSM command but that should be plaintext and from my tests there is not white space coming through.
#!/bin/bash aws ec2 reboot-instances --instance-ids `aws ec2 describe-instances --filters "Name=tag:RebootGroup,Values=01" --query 'Reservations[].Instances[].InstanceId' --output text` sleep 30 upTime=1 while [[ $upTime -le 120 ]] do ssmID=$(aws ssm send-command --document-name "AWS-RunPowerShellScript" --document-version "1" --targets '[{"Key":"tag:RebootGroup","Values":["01"]}]' --parameters '{"commands":["$wmi = Get-WmiObject -Class Win32_OperatingSystem ","$uptimeSeconds = ($wmi.ConvertToDateTime($wmi.LocalDateTime)-$wmi.ConvertToDateTime($wmi.LastBootUpTime) | select-object -expandproperty \"TotalSeconds\")","[int]$uptimeSeconds"],"workingDirectory":[""],"executionTimeout":["3600"]}' --timeout-seconds 600 --max-concurrency "50" --max-errors "0" --region us-west-2 --output text --query "Command.CommandId") echo "Value of ssmID = $ssmID" echo "Value of upTime before sleep = $upTime" sleep 15 upTime=$(aws ssm list-command-invocations --command-id "$ssmID" --details --query "CommandInvocations[].CommandPlugins[].{Output:Output}" --output text) echo "Value of upTime after command invocation = $upTime" echo "Waiting for instance uptime of 2 minutes before continuing" done
echo
doesn't do a good job of showing nonprinting, whitespace, etc characters; try adding delimiters around the value, and usingcat -vt
to make weird characters visible. For example:echo "Value of upTime after command invocation = <$upTime>" | | LC_ALL=C cat -vt