Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

bash - Jenkins Job failing while checking service status

We are running a jenkins job which runs a script to check the status of the kafka service. The kakfa instances are kept in for loop. when the kafka service is running it checks in all kafka instances and gives job as success, when any one of the kafka service fails, jenkins job gets exits, so we could not know the service status of other running instances. So can we get a solution to check the status of other kafka service also if any one of the service is not running.


pipeline {
    agent { label 'host1' }
 
    options {
            skipDefaultCheckout()
            }
    stages {
        stage('Checkout code') {
            steps {
                checkout scm
            }
        }
    stage('Build') {
            steps {
                script{
                    dir("$env.WORKSPACE"){
                    sh  '''                     
 
                        check_status() {
                            
                            serviceCheck=$(/etc/init.d/kafka status | awk '{print $3}') >/dev/null
 
                            if [[ "$serviceCheck" = "Running" ]]; then
                                echo "Kafka Service is running "
                            else
                                echo "Kafka Service is NOT running"
                                
                            fi
                        }
                        cd ../
                        kafka_error_state=0
                        hostIPList=("xxx.compute-1.amazonaws.com" "yyy.compute-1.amazonaws.com" "zzz.compute-1.amazonaws.com")
                        for hostIP in "${hostIPList[@]}"; do
                            echo " **** checking  kafka service status in $hostIP ****"
                            sudo ssh -i "kafka.pem" ubuntu@$hostIP "$(typeset -f check_status); check_status" 
                            
                            if [ $? -eq 10 ]; then
                                kafka_error_state=1
                            fi
                        done
 
                        if [ "$kafka_error_state" -eq 1 ]; then
                            echo "Please check logs, kafka is not running in one or more of the instances.."
                            exit 1
                        fi
 
                    '''
                        
                    }
                }
            }
           
        }
     }
       

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...