Home > Back-end >  How do i restart a service which is in a remote server using Jenkins pipeline steps
How do i restart a service which is in a remote server using Jenkins pipeline steps

Time:01-17

Here is my issue I have a service(nhasi. service) running on a remote Linux server,, I have created my Jenkins pipeline which copies my jar file to the remote server. So I want when copying file is done, the nhasi.service has to be restarted. I have tried the command below

sh 'sudo systemctl restart nhasi.service'

but I am getting the following errorsystemctl: command not found

My Jenkins server is running on a windows server

CodePudding user response:

sh 'sudo systemctl restart nhasi.service' will execute the command in the Jenkins machine itself. So you have SSH into the remote machine and execute the commans. You can use something like SSH Step for this.

def remote = [:]
    remote.name = 'test'
    remote.host = 'test.domain.com'
    remote.user = 'root'
    remote.password = 'password'
    remote.allowAnyHosts = true
    stage('Remote SSH') {
      sshCommand remote: remote, command: "systemctl restart nhasi.service"
    }

Another option is to add the remote server as a Jenkins agent and execute the command on the remote Agent.

CodePudding user response:

Thank you @ycr, your answer helped to find the final solution , i implemented as below

stage("Restarting Service") {
    
steps{
    script{
    def remote = [:]
    remote.name = 'test'
    remote.host = 'test.domain.com'
    remote.user = 'root'
    remote.password = 'password'
    remote.allowAnyHosts = true
    sshCommand remote: remote, command: "systemctl restart nhasi.service"
    }
    
}
}
  • Related