단일 시점에 여러 원격 시스템에서 단일 명령을 실행하는 방법

단일 시점에 여러 원격 시스템에서 단일 명령을 실행하는 방법

3개의 다른 컴퓨터에서 성능 테스트를 트리거하기 위해 루프에서 실행되는 다음 셸 스크립트가 있습니다. 3대의 컴퓨터에서 유효성을 검사한 후 동시에 테스트를 실행하고 싶습니다. 동시에 3대의 컴퓨터에 컬 명령을 보내는 것과 같습니다.

#!/bin/sh

IP_Addresses=(192.168.2.33,192.168.2.34,192.168.2.35)

triggerPerformanceTest(){ 
    for iplist in $(echo $IP_Addresses | sed "s/,/ /g")
    do
        echo "Validating the health end point in the remote server"    
        HTTP_RESPONSE=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
        HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
        HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
            if [ $HTTP_STATUS -eq 200  ] && [ $HTTP_BODY = "OK" ]; then
                echo "Success!! it seems the health service is ruuning in the target machine"
                echo "Starting the Jmeter Test"
            else
                echo "The health end point of the nodejs service return exit code 1, let the service start on the remote host"
                sleep 20
                HTTP_RESPONSE_NEW=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
                HTTP_STATUS_NEW=$(echo $HTTP_RESPONSE_NEW | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
                HTTP_BODY_NEW=$(echo $HTTP_RESPONSE_NEW | sed -e 's/HTTPSTATUS\:.*//g')
                count=2
                echo "Re-validating the health end point in the remote server"
                    while [ $HTTP_STATUS_NEW -ne 200 ] && [ $HTTP_BODY_NEW != "OK" ]
                    do
                        echo "Waiting for the Node-js service to start on the remote server"
                        sleep 5
                        HTTP_RESPONSE_FINAL=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
                        HTTP_STATUS_FINAL=$(echo $HTTP_RESPONSE_NEW | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
                        HTTP_BODY_FINAL=$(echo $HTTP_RESPONSE_NEW | sed -e 's/HTTPSTATUS\:.*//g')

                        count=`expr $count + 1`
                            if [ $count -eq 0 ]
                            then   
                                break
                            else
                                continue
                            fi
                    done
                        echo "Starting the Jmeter Test"
            fi
    done
}

triggerPerformanceTest $IP_Addresses

그래서 내 계획은 이 세 가지 컬 명령을 배열 요소에 넣고 호출하는 것입니다. 하지만 이제는 차례로 호출됩니다. 3대의 컴퓨터에서 병렬로 트리거할 수 있는 방법이 있습니까? 다음 명령을 한꺼번에 호출하는 것과 같습니다.

curl http://192.168.2.33:9096/triggerPerformanceTest
curl http://192.168.2.34:9096/triggerPerformanceTest
curl http://192.168.2.35:9096/triggerPerformanceTest

아니면 이 문제를 해결하는 더 좋은 방법이 있습니까? 내가 생각할 수 있는 것은 대기 시간 테스트를 트리거하는 논리를 대상 컴퓨터에 구현하는 것입니다. 즉, 첫 번째 컴퓨터는 몇 밀리초만큼 테스트 대기 시간을 가지며 두 번째 컴퓨터는 첫 번째 및 세 번째 컴퓨터보다 짧은 대기 시간을 갖습니다(0). .

답변1

이 시도,

#!/bin/bash

IP_Addresses=(192.168.2.33 192.168.2.34 192.168.2.35)

triggerPerformanceTest(){ 
        iplist=$1
        LOG=/tmp/$iplist.log
        echo "Validating the health end point in the remote server"  >> $LOG
        HTTP_RESPONSE=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
        HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
        HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
            if [ $HTTP_STATUS -eq 200  ] && [ $HTTP_BODY = "OK" ]; then
                echo "Success!! it seems the health service is ruuning in the target machine" >> $LOG
                echo "Starting the Jmeter Test" >> $LOG
            else
                echo "The health end point of the nodejs service return exit code 1, let the service start on the remote host" >> $LOG
                sleep 20
                HTTP_RESPONSE_NEW=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
                HTTP_STATUS_NEW=$(echo $HTTP_RESPONSE_NEW | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
                HTTP_BODY_NEW=$(echo $HTTP_RESPONSE_NEW | sed -e 's/HTTPSTATUS\:.*//g')
                count=2
                echo "Re-validating the health end point in the remote server" >> $LOG
                    while [ $HTTP_STATUS_NEW -ne 200 ] && [ $HTTP_BODY_NEW != "OK" ]
                    do
                        echo "Waiting for the Node-js service to start on the remote server" >> $LOG
                        sleep 5
                        HTTP_RESPONSE_FINAL=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
                        HTTP_STATUS_FINAL=$(echo $HTTP_RESPONSE_NEW | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
                        HTTP_BODY_FINAL=$(echo $HTTP_RESPONSE_NEW | sed -e 's/HTTPSTATUS\:.*//g')
                        $HTTP_STATUS_NEW=$((HTTP_STATUS_FINAL))
                        $HTTP_BODY_NEW=$((HTTP_BODY_FINAL))
                        count=`expr $count + 1`
                            if [ $count -eq 0 ]
                            then   
                                break
                            else
                                continue
                            fi
                    done
                        echo "Starting the Jmeter Test" >> $LOG
            fi
}
for ip in ${IP_Addresses[@]}
do
    triggerPerformanceTest "$ip" &
done

예상대로 작동하는지 알려주세요.

echo메시지는 다음 위치에 있습니다./tmp/$iplist.log

(편집 중...OP의 답변을 기다리는 중)

답변2

가장 간단한 해결책은 루프를 함수 밖으로 이동하고 백그라운드에서 함수를 실행하는 것입니다.

#!/bin/sh


triggerPerformanceTest(){ 
  thisIp=$1
  ## the rest of your tests. Just use $thisIp the way you were using
  ## the $iplist variable.
}

IP_Addresses="192.168.2.33,192.168.2.34,192.168.2.35"
for ip in $IP_Addresses; do
    triggerPerformanceTest "$ip" &
}

그러면 각 IP에서 테스트가 시작되고 백그라운드로 보내기 명령을 사용하므로 테스트가 시작 &됩니다.거의동시에. 밀리초 정밀도가 필요하지 않다면 이것으로 충분합니다.

답변3

이미 답변을 수락했지만. 제 생각에는 parallel다음 명령을 사용하여 이를 달성 할 수도 있습니다.

#!/bin/sh

IP_Addresses=(192.168.2.33,192.168.2.34,192.168.2.35)

triggerPerformanceTest(){ 
    for iplist in $(echo $IP_Addresses | sed "s/,/ /g")
    do
        echo "Validating the health end point in the remote server"    
        HTTP_RESPONSE=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
        HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
        HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
            if [ $HTTP_STATUS -eq 200  ] && [ $HTTP_BODY = "OK" ]; then
                echo "Success!! it seems the health service is ruuning in the target machine"
                echo "Starting the Jmeter Test"
            else
                echo "The health end point of the nodejs service return exit code 1, let the service start on the remote host"
                sleep 20
                HTTP_RESPONSE_NEW=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
                HTTP_STATUS_NEW=$(echo $HTTP_RESPONSE_NEW | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
                HTTP_BODY_NEW=$(echo $HTTP_RESPONSE_NEW | sed -e 's/HTTPSTATUS\:.*//g')
                count=2
                echo "Re-validating the health end point in the remote server"
                    while [ $HTTP_STATUS_NEW -ne 200 ] && [ $HTTP_BODY_NEW != "OK" ]
                    do
                        echo "Waiting for the Node-js service to start on the remote server"
                        sleep 5
                        HTTP_RESPONSE_FINAL=$(curl -m 1800 --silent --write-out "HTTPSTATUS:%{http_code}" -X GET http://${iplist}:9096/health)
                        HTTP_STATUS_FINAL=$(echo $HTTP_RESPONSE_NEW | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
                        HTTP_BODY_FINAL=$(echo $HTTP_RESPONSE_NEW | sed -e 's/HTTPSTATUS\:.*//g')

                        count=`expr $count + 1`
                            if [ $count -eq 0 ]
                            then   
                                break
                            else
                                continue
                            fi
                    done
                        echo "Starting the Jmeter Test"
            fi
    done
}

cat IP_Addresses_file | parallel triggerPerformanceTest {}

IP_Addresses_file모든 IP 주소를 포함하는 파일이 있다고 가정합니다 .

관련 정보