Home > Enterprise >  Find Jenkin jobs running more than 24 hours
Find Jenkin jobs running more than 24 hours

Time:10-03

I would like to find the Jenkin jobs in the master and client running more than 5 hours and kill it.

I tried multiple options using Thread.getAllStackTraces() and list all the jobs it was not helpful. Your help is much appreciated.

CodePudding user response:

Use timeout

pipeline {
    options {
          timeout(time: 5, unit: 'HOURS')
    }
    stages {
          steps {
                sh '...'
          }
    }
}

CodePudding user response:

Get all Jenkins jobs and their builds. Go through the list and check if there are builds running. If you find a build that is running, you can retrieve its time/timestamp/duration. Based on that time you could determine does that build is already running longer than X hours.

jenkins.model.Jenkins.instance.getAllItems(AbstractItem.class). each {
  println "Job: "   it.fullName   " - "   it.class   " is running: "   it.isBuilding()
  if(it.isBuilding()) {
    def builds = it.getBuilds().each {
      if(it.isBuilding()) { 
        println it.getTime()   //getTimestamp(), getTimeInMillis(), getDuration() ...
      }
    }
  }
}

WorkflowJob Docu
WorkflowRun Docu
Jenkins Core API

  • Related