Veo algunos problemas con el flujo del programa básico aquí. Donde tiene una construcción aquí, realmente hay un par de cosas que se están rastreando:
Así que usaría dos bucles para este script. Además, continue
no debería ser necesario aquí, y eso es probablemente lo que está causando que se repita el primer host cada vez.
Hice algunas cosas de manera diferente, principalmente probando la cantidad de PID devueltos desde psget
en lugar de verificar el código de salida ssh, y usando una sintaxis ligeramente diferente. Aquí está el ejemplo que se me ocurrió que parece funcionar para lo que quieres:
#!/bin/bash
set -eu
# get number of IPs from lines in file
NUM_IPS=$( cat ip.txt | wc -l )
echo "checking ${NUM_IPS} hosts..."
# Set number of running hosts to the max. While arbitrary, it will
# update to the correct number before reporting, and if it is 0 the
# while loop will exit immediately.
IPS_STILL_RUNNING=${NUM_IPS}
while [ "${IPS_STILL_RUNNING}" -gt "0" ]
do
RUNNING_NOW=0
for IP in $( cat ip.txt )
do
PROC_NUM=$( ssh ${IP} -- pgrep -f pattern | wc -l )
if [ "${PROC_NUM}" -gt "0" ]; then
echo " ${IP}: still running"
RUNNING_NOW=$(( RUNNING_NOW + 1 ))
else
echo " ${IP}: not running"
fi
done
echo "still running on $RUNNING_NOW hosts"
IPS_STILL_RUNNING=${RUNNING_NOW}
sleep 10
done