I had to monitor the execution of N containers that were collecting data from multiple accounts at IBM Cloud. Here, each container is simulation the process to collect the data, one container per account:
#!/bin/bash
# read a file with the login credentials
while IFS= read -r line; do
clean_line=$(echo "$line" | tr -d '\r')
ACCOUNTS+=("$clean_line")
done < ../cloud_accounts
# starts a container to collect date from each account
for account in "${ACCOUNTS[@]}"; do
TIME=$(($RANDOM % 3 + 1))
CLOUD=$(echo "$account" | awk -F "," '{print $1}')
CLOUD_ID=$(echo "$CLOUD" | awk -F ":" '{print $1}')
container_id=$(docker run -d -t --rm --name "vm-collector-$CLOUD_ID" ubuntu:20.04 sleep ${TIME}m)
CONTAINER_IDS+=("$container_id")
done
# starts monitoring and controlling the execution
rm -f ./.containers_id
for cid in "${CONTAINER_IDS[@]}"; do
echo "$cid" >> ./.containers_id
done
while [ ${#CONTAINER_IDS[@]} != 0 ]; do
IFS=$'\n' read -d '' -r -a CONTAINER_IDS < ./.containers_id
echo -ne "(Running ${#CONTAINER_IDS[@]} containers...) \033[0K\r"
for cid in "${CONTAINER_IDS[@]}"; do
if [ ! "$(docker ps -q -f id="$cid")" ]; then
sed -i -e "/$cid/d" ./.containers_id
fi
done
done