254

Our Jenkins server has a job that has been running for three days, but is not doing anything. Clicking the little X in the corner does nothing, and the console output log doesn't show anything either. I've checked on our build servers and the job doesn't actually seem to be running at all.

Is there a way to tell jenkins that the job is "done", by editing some file or lock or something? Since we have a lot of jobs we don't really want to restart the server.

blokkie
  • 5,375
  • 7
  • 24
  • 18
  • 3
    Seems with recent versions of Jenkins the solution is not the one marked as accepted. (but the one from '16) – NicolasW Mar 06 '18 at 14:37

30 Answers30

353

I had also the same problem and fix it via Jenkins Console.

Go to "Manage Jenkins" > "Script Console" and run a script:

Jenkins.instance.getItemByFullName("JobName")
        .getBuildByNumber(JobNumber)
        .finish(hudson.model.Result.ABORTED, 
                new java.io.IOException("Aborting build")
); 

You'll have just specify your JobName and JobNumber.

tamerlaha
  • 1,902
  • 1
  • 17
  • 25
Alexandru Bantiuc
  • 3,707
  • 2
  • 10
  • 6
  • I had this with a Pipeline job that started other jobs. The server crashed, the other jobs were gone, but the pipeline job was still a zombie. I first tried the accepted answer, to no avail. I had to run @Alexandru's command several times, each time I saw the progress bar of the pipeline job move a bit. Finally the pipeline job had died and for good measures I deleted it too. – Amedee Van Gasse Oct 14 '16 at 10:00
  • 32
    This works great for multi-branch projects as well but the key is to specify the JobName as Jenkins.instance.getItemByFullName("/") – evasilchenko Dec 06 '16 at 18:59
  • 30
    This answer helped me to resolve my problem. The pipeline was a total zombie. The above script haven't worked and pipeline was still running even after a few jenkins restarts. I read some internal class documentation and found a delete() method so my script looked like this: `Jenkins.instance.getItemByFullName("JobName").getBuildByNumber(JobNumber).delete();` After executing this and one another jenkins restart the zombie build was finally gone. – Szymon Sadło Apr 25 '17 at 08:22
  • 1
    @evasilchenko and if the branch ifself contains slash (url-)escape it with %2F – Bludwarf Jul 11 '17 at 15:28
  • Any tips on how to automatically discover the problematic job names and build numbers? Due to a particularly untimely Jenkins restart, I have about 100 zombie jobs, each one with a different build number. – jayhendren Nov 28 '17 at 19:11
  • I figured out how to loop over all zombie jobs. It's too long for a comment, so I wrote it up as an answer: https://stackoverflow.com/a/47540001/1824868 – jayhendren Nov 28 '17 at 20:07
  • 6
    There is not method `finish` in AbstractBuild nor FreeSyleBuild nor MavenModulesetBuild – Jakub Bochenski Jan 10 '18 at 15:23
  • Just be careful and get the correct "JobNumber" from the Build History on the Job itself. Took me a while. :) – Georgi Mihaylov May 30 '18 at 06:46
  • 10
    I got issue when execute this script, any idea? `groovy.lang.MissingMethodException: No signature of method: hudson.model.FreeStyleBuild.finish() is applicable for argument types: (hudson.model.Result, java.io.IOException) values: [ABORTED, java.io.IOException: Aborting build] Possible solutions: find(), findAll(), find(groovy.lang.Closure) at` – Tien Dung Tran Aug 30 '18 at 02:43
  • 3
    JobName: Open Project Page and notice text for 'Full project name' – dtmland Sep 14 '18 at 20:10
  • I cannot execute this script, did I do something wrong ? java.lang.NullPointerException: Cannot invoke method getBuildByNumber() on null object at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.jav – Tien Dung Tran Nov 23 '18 at 07:33
  • @TienDungTran What is the jobs name, is it in a folder, and what is the result of `println(Jenkins.instance.getItem("JOBNAME"))` or `println(Jenkins.instance.getItem("FULLJOBNAME"))`? – t0r0X Nov 30 '18 at 22:14
  • @t0r0X this job in a folder, job name is "sonar_desk_individual_customer1", this is a maven project. Both command return null, do I get incorrect job name? – Tien Dung Tran Dec 03 '18 at 08:20
  • @TienDungTran Try `println(Jenkins.instance.getItemByFullName("FOLDERNAME/JOBNAME"))` – t0r0X Dec 05 '18 at 19:36
  • 3
    I got another problem after I passed correct job name and job number? groovy.lang.MissingMethodException: No signature of method: hudson.maven.MavenModuleSetBuild.finish() is applicable for argument types: (hudson.model.Result, java.io.IOException) values: [ABORTED, java.io.IOException: Aborting build] Possible solutions: find(), findAll(), find(groovy.lang.Closure) – Tien Dung Tran Dec 10 '18 at 04:28
  • 2
    @SzymonSadio I noticed with delete() it was removed from the Build History view but it was still hanging in the executors, so this did not resolve it for me unfortunately. I am also getting the below using the finish() method: `groovy.lang.MissingMethodException: No signature of method: hudson.model.FreeStyleBuild.finish() is applicable for argument types: (hudson.model.Result, java.io.IOException)` – lkisac Jul 28 '20 at 14:39
  • Also doesn't work for me. 'Jenkins .instance.getItemByFullName("youtube-downloadmac") .getBuildByNumber(5) .finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));' result - java.lang.NullPointerException: Cannot invoke method getItemByFullName() on null object – majorgear May 30 '22 at 20:33
  • This stopped the job on the master node, but not the agent – not2savvy Feb 08 '23 at 14:52
286

Go to "Manage Jenkins" > "Script Console" to run a script on your server to interrupt the hanging thread.

You can get all the live threads with Thread.getAllStackTraces() and interrupt the one that's hanging.

Thread.getAllStackTraces().keySet().each() {
  t -> if (t.getName()=="YOUR THREAD NAME" ) {   t.interrupt();  }
}

UPDATE:

The above solution using threads may not work on more recent Jenkins versions. To interrupt frozen pipelines refer to this solution (by alexandru-bantiuc) instead and run:

Jenkins.instance.getItemByFullName("JobName")
                .getBuildByNumber(JobNumber)
                .finish(
                        hudson.model.Result.ABORTED,
                        new java.io.IOException("Aborting build")
                );
LoganMzz
  • 1,597
  • 3
  • 18
  • 31
Zahra
  • 6,798
  • 9
  • 51
  • 76
  • 52
    Worked great! For anyone reading, you can view the thread names by first running the above, with the method calling `t -> println(t.getName());` – Phil Apr 22 '15 at 21:32
  • 2
    Still its not working with Above script too, Its getting the scripts but not killing the same. – Raghav S Apr 29 '15 at 13:45
  • 2
    are you able to print the name of the specific thread after matching the name in `t.getName()=="SOME NAME"` ? – Zahra Apr 29 '15 at 20:20
  • 3
    This does not help me either - the thread does not react to the interrupt(). – Zitrax Nov 13 '15 at 09:33
  • ```Thread.getAllStackTraces().keySet().each() { t -> println(t.getName()); }``` – Yuvaraj Loganathan Jul 08 '16 at 07:00
  • 1
    Doesn't work either. I can't see hanged threads with the 2 pipeline runs that are stuck. Restart doesn't help either. – Ilia Shakitko May 19 '17 at 11:53
  • 3
    for me interrupt was not enough, i needed to call `t.stop` instead: `Thread.getAllStackTraces().keySet().each() { t -> if (t.getName()=="YOUR THREAD NAME" ) { println(“Found, stopping now… “); t.stop(); } }` – friday Aug 21 '18 at 09:00
  • If you are using a **Multi-Branch pipeline**, see [answer](https://stackoverflow.com/a/48279524/701803) by Markus Schulte below – tvt173 Sep 14 '18 at 01:14
  • 1
    To use the update with jobs in folders: "job/myfolder/job/jobname". Use Jenkins.instance.getItemByFullName("myfolder/jobname") – lvthillo Jun 25 '19 at 08:35
  • i.e. Jenkins.instance.getItemByFullName("$ProjectName/$ProjectBranch") .getBuildByNumber($numberOfJob) .finish( hudson.model.Result.ABORTED, new java.io.IOException("Aborting build") ); – wan_keen Jul 11 '19 at 10:47
  • It effectively stops the running viewer, giving you the opportunity to launch a new one. But the underlying elements (docker run...) are still running and logging into docker. – Sandburg Aug 23 '19 at 12:35
  • Thanks. The code in the UPDATE part worked for my 2.164.3 version of Jenkins – Chris F Nov 19 '19 at 19:51
  • work for me, but after execute the script was necesary manual action to stop che job, (and this time work) no more ghost on my jenkins – Gianluca Musa Apr 28 '20 at 11:59
  • As for how to find the JobName to insert, I referenced the Full project name and it worked thank you! – HighlanderGrogg Sep 16 '20 at 16:39
  • 2
    On Jenkins 2.235.5 With the UPDATEd solution, I get `groovy.lang.MissingMethodException: No signature of method: hudson.model.FreeStyleBuild.finish() is applicable for argument types: (hudson.model.Result, java.io.IOException) values: [ABORTED, java.io.IOException: Aborting build] Possible solutions: find(), findAll(), find(groovy.lang.Closure)` – Nikhil Owalekar Mar 25 '21 at 08:29
68

Without having to use the script console or additional plugins, you can simply abort a build by entering /stop, /term, or /kill after the build URL in your browser.

Quoting verbatim from the above link:

Pipeline jobs can by stopped by sending an HTTP POST request to URL endpoints of a build.

  • <BUILD ID URL>/stop - aborts a Pipeline.
  • <BUILD ID URL>/term - forcibly terminates a build (should only be used if stop does not work.
  • <BUILD ID URL>/kill - hard kill a pipeline. This is the most destructive way to stop a pipeline and should only be used as a last resort.
Dibakar Aditya
  • 3,893
  • 1
  • 14
  • 25
60

In case you got a Multibranch Pipeline-job (and you are a Jenkins-admin), use in the Jenkins Script Console this script:

Jenkins.instance
.getItemByFullName("<JOB NAME>")
.getBranch("<BRANCH NAME>")
.getBuildByNumber(<BUILD NUMBER>)
.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));

From https://issues.jenkins-ci.org/browse/JENKINS-43020

If you aren't sure what the full name (path) of the job is, you may use the following snippet to list the full name of all items:

  Jenkins.instance.getAllItems(AbstractItem.class).each {
    println(it.fullName)
  };

From https://support.cloudbees.com/hc/en-us/articles/226941767-Groovy-to-list-all-jobs

StockB
  • 755
  • 1
  • 13
  • 33
Markus Schulte
  • 4,171
  • 3
  • 47
  • 58
  • side note to this: if you are using SVN (and you follow the standard conventions), your will be something like *branches/my_branch* – tvt173 Sep 14 '18 at 01:17
  • Note: to view the job name you can also go to the job's main page and use the "Full project name: " – lkisac Jul 28 '20 at 14:35
  • Life Saver. Perfect solution for Multi-branch pipeline or Bitbucket projects – bhr Oct 27 '20 at 21:07
33

The first proposed solution is pretty close. If you use stop() instead of interrupt() it even kills runaway threads, that run endlessly in a groovy system script. This will kill any build, that runs for a job. Here is the code:

Thread.getAllStackTraces().keySet().each() {
    if (it.name.contains('YOUR JOBNAME')) {  
      println "Stopping $it.name"
      it.stop()
    }
}
funql.org
  • 408
  • 4
  • 9
  • 4
    IMO that should be the accepted answer. All the other answers didn't work for me, as the build was already in an interrupted state, but that hanging in some post build step. Only this solution really did stop the build – Kutzi Jan 25 '18 at 13:06
  • 1
    Using `contains` here is incorrect and dangerous - if your job's name is "Run Tests", it will also kill any jobs named "Run Tests - Integration", "Run Tests - Unit", etc. Anyone using this will need to be careful not to terminate unrelated jobs unexpectedly – Brandon Mar 18 '19 at 21:13
31

Once I encounterred a build which could not be stopped by the "Script Console". Finally I solved the problem with these steps:

ssh onto the jenkins server
cd to .jenkins/jobs/<job-name>/builds/
rm -rf <build-number>
restart jenkins
mugi
  • 439
  • 4
  • 8
  • that actually helped in my case: the job didn't exist anymore at the time of killing it via the console (dynamic pipeline job, feature branch deleted) – mkko Dec 08 '16 at 17:08
  • 2
    should be the accepted answer, i tried almost all others, this one works like a charm. – emarshah Dec 14 '20 at 23:49
  • This is the only solution that works for me. A stuck Jenkins job for a full day, none of the other solutions work. Thank you a lot @mugi. – Juliano Macedo Jul 31 '21 at 13:08
27

I use the Monitoring Plugin for this task. After the installation of the plugin

  1. Go to Manage Jenkins > Monitoring of Hudson/Jenkins master
  2. Expand the Details of Threads, the small blue link on the right side
  3. Search for the Job Name that is hung

    The Thread's name will start like this

    Executor #2 for master : executing <your-job-name> #<build-number>

  4. Click the red, round button on the very right in the table of the line your desired job has

cheffe
  • 9,345
  • 2
  • 46
  • 57
  • 3
    It says as killed, But again when we refresh the page the thread seems to be alive – Raghav S Apr 29 '15 at 13:46
  • Interesting. I will have a look at this. Probably it depends on the build. In case you have started external processes, probably by ANT or Maven extensions, this might fail. – cheffe May 11 '15 at 09:04
  • This is the solution that worked for me. Just got into the list of thread, did a search for the name of the job and clicked the red button. https://jenkinsServer/monitoring#threads – Gilberto Treviño Sep 25 '19 at 17:22
17

If you have an unstoppable Pipeline job, try the following:

  1. Abort the job by clicking the red X next to the build progress bar
  2. Click on "Pause/resume" on the build to pause
  3. Click on "Pause/resume" again to resume the build

Pause/Resume pipeline job

Jenkins will realize that the job should be terminated and stops the build

Levente Holló
  • 714
  • 6
  • 13
11

I guess it is too late to answer but my help some people.

  1. Install the monitoring plugin. (http://wiki.jenkins-ci.org/display/JENKINS/Monitoring)
  2. Go to jenkinsUrl/monitoring/nodes
  3. Go to the Threads section at the bottom
  4. Click on the details button on the left of the master
  5. Sort by User time (ms)
  6. Then look at the name of the thread, you will have the name and number of the build
  7. Kill it

I don't have enough reputation to post images sorry.

Hope it can help

Coulemelle
  • 341
  • 1
  • 4
  • 13
  • 1
    Not helping, It says killed. but again when page reloads I am able to see that Thread – Raghav S Apr 29 '15 at 14:04
  • Are you kill the thread of the build or a subthread of the build ? What is the name of this thread ? I guess you don't kill the good one. If you kill the thread of the build, you will see the build finished successfully. – Coulemelle May 03 '15 at 11:30
  • 2
    I tried killing the Thread which is associated with the executor number of slave which also had the job name. Also I found several other threads associated with Handling GET and the information contained was with respect to Subversion. Killing both also did not help. Finally restart helped me. One more observation was, Other threads without SVN association was killable. – Raghav S May 03 '15 at 12:39
  • This answer is a copy of @cheffe answer, which was posted one month earlier. – t0r0X Oct 10 '18 at 17:33
8

The top answer almost worked for me, but I had one major problem: I had a very large number (~100) of zombie jobs due to a particularly poorly-timed Jenkins restart, so manually finding the job name and build number of each and every zombie job and then manually killing them was infeasible. Here's how I automatically found and killed the zombie jobs:

Jenkins.instance.getItemByFullName(multibranchPipelineProjectName).getItems().each { repository->
  repository.getItems().each { branch->
    branch.builds.each { build->
      if (build.getResult().equals(null)) {
        build.doKill()
      }
    }
  }
}

This script loops over all builds of all jobs and uses getResult().equals(null) to determine whether or not the job has finished. A build that's in the queue but not yet started will not be iterated over (since that build won't be in job.builds), and a build that's finished already will return something other than null for build.getResult(). A legitimately running job will also have a build result of null, so make sure you have no running jobs that you don't want to kill before running this.

The multiple nested loops are mainly necessary to discover every branch/PR for every repository in a Multibranch Pipeline project; if you're not using Multibranch Pipelines you can just loop over all your jobs directly with something like Jenkins.instance.getItems().each.

jayhendren
  • 4,286
  • 2
  • 35
  • 59
  • 3
    I slightly improved your script. `runningBuilds = Jenkins.instance.getView('All').getBuilds().findAll() { it.getResult().equals(null) } runningBuilds.each { branch->branch.doKill() }` – Tobi Jan 10 '19 at 07:49
  • I get `groovy.lang.MissingPropertyException: No such property: multibranchPipelineProjectName for class: Script1 ` – Chris F Nov 16 '20 at 15:04
  • That's just an example variable name I used. You have to fill it in with the name of your own multibranch pipeline project. – jayhendren Nov 16 '20 at 19:24
7

Build-timeout Plugin can come handy for such cases. It will kill the job automatically if it takes too long.

Draco Ater
  • 20,820
  • 8
  • 62
  • 86
  • 1
    Unfortunately that's not an option for us, because we have a couple of jobs that are supposed to run for days (don't ask) – blokkie Jan 23 '13 at 14:59
  • 7
    You configure build timeouts on per job basis. – Draco Ater Jan 23 '13 at 15:01
  • 1
    No, we have a build stuck for over 3 hours with a timeout set to 95 minutes I don't think the timeout plugin can help as it's doing the same as clicking "Abort" manually – Jakub Bochenski Jan 10 '18 at 15:21
5

I've looked at the Jenkins source and it appears that what I'm trying to do is impossible, because stopping a job appears to be done via a Thread interrupt. I have no idea why the job is hanging though..

Edit:

Possible reasons for unstoppable jobs:

  • if Jenkins is stuck in an infinite loop, it can never be aborted.
  • if Jenkins is doing a network or file I/O within the Java VM (such as lengthy file copy or SVN update), it cannot be aborted.
blokkie
  • 5,375
  • 7
  • 24
  • 18
  • This is actually not impossible. You can use jenkins script console to interrupt the thread that is running your job. See the explanation here: http://stackoverflow.com/a/26306081/1434041 – Zahra May 26 '15 at 19:22
5

Alexandru Bantiuc's answer worked well for me to stop the build, but my executors were still showing up as busy. I was able clear the busy executor status using the following

server_name_pattern = /your-servers-[1-5]/
jenkins.model.Jenkins.instance.getComputers().each { computer ->
  if (computer.getName().find(server_name_pattern)) {
    println computer.getName()
    execList = computer.getExecutors()      
    for( exec in execList ) {
      busyState = exec.isBusy() ? ' busy' : ' idle'
      println '--' + exec.getDisplayName() + busyState
      if (exec.isBusy()) {
        exec.interrupt()
      }
    }
  }
}
austinfromboston
  • 3,791
  • 25
  • 25
5

Recently I came across a node/agent which had one executor occupied for days by a build "X" of a pipeline job, although that jobs page claimed build "X" did not exist anymore (discarded after 10 subsequent builds (!), as configured in the pipeline job). Verified that on disk: build "X" was really gone.

The solution: it was the agent/node which wrongly reported that the occupied executor was busy running build "X". Interrupting that executor's thread has immediately released it.

def executor = Jenkins.instance.getNode('NODENAME').computer.executors.find {
    it.isBusy() && it.name.contains('JOBNAME')
}

println executor?.name
if (executor?.isBusy()) executor.interrupt()

Other answers considered:

  • The answer from @cheffe: did not work (see next point, and update below).
  • The answers with Thread.getAllStackTraces(): no matching thread.
  • The answer from @levente-holló and all answers with getBuildByNumber(): did not apply as the build wasn't really there anymore!
  • The answer from @austinfromboston: that came close to my needs, but it would also have nuked any other builds running at the moment.

Update:
I experienced again a similar situation, where a Executor was occupied for days by a (still existing) finished pipeline build. This code snippet was the only working solution.

t0r0X
  • 4,212
  • 1
  • 38
  • 34
  • This did the trick for me, thanks! The other solutions were not working as the build number was already be thrown away (we just keep the lat 5 builds, so job.getBuildByNumber(...) did not return anything). – L. Tischler Jan 16 '19 at 07:58
  • What if executor is busy due to a real in progress build? – JRichardsz Mar 03 '23 at 19:58
  • @JRichardsz You have to analyze the situation and decide accordingly. If you can't stop the build by other means (e.g. "interrupt build" button), then I'd try to kill the threads managing the build, see e.g. the answers from @alexandru-bantiuc and @zahra, AND the externally spawned processes. Depending on the OS you might try e.g. `ps fux` for a Linux processes tree. If you're building with containers instead of "native" build agents, then you'd have to identify the container(s) belonging to the build and stop that container. I hope I undertood your question correctly. – t0r0X Mar 05 '23 at 22:32
4

Had this same issue but there was not stack thread. We deleted the job by using this snippet in the Jenkins Console. Replace jobname and buil dnumber with yours.

def jobname = "Main/FolderName/BuildDefinition"
def buildnum = 6
Jenkins.instance.getItemByFullName(jobname).getBuildByNumber(buildnum).delete(); 
Kenneth King
  • 126
  • 1
  • 2
4

This works for me everytime:

Thread.getAllStackTraces().keySet().each() {
    if (it.name.contains('YOUR JOBNAME')) {  
      println "Stopping $it.name"
      it.stop()
    }
}

Thanks to funql.org

Aviel Yosef
  • 533
  • 5
  • 10
3

I usually use jenkins-cli in such cases. You can download the jar from a page http://your-jenkins-host:PORT/cli . Then run

java -jar jenkins-cli.jar delete-builds name_of_job_to_delete hanging_job_number

Auxiliary info:

You may also pass a range of builds like 350:400. General help available by running

java -jar jenkins-cli.jar help

Context command help for delete-builds by

java -jar jenkins-cli.jar delete-builds
Krzysztof Jabłoński
  • 1,890
  • 1
  • 20
  • 29
3

I had same issue at the last half hour...

Was not able to delete a zombie build running in my multi-branch pipeline. Even Server restarts by UI or even from commandline via sudo service jenkins restart did block the execution... The build was not stoppable... It always reapeared.

Used Version: Jenkins ver 2.150.2

I was very annoyed, but... when looking into the log of the build I found something intersting at the end of the log:

Logfile output of an zombie build and showing restart did not stop it

The red marked parts are the "frustrating parts"... As you can see I always wanted to Abort the build from UI but it did not work...

But there is a hyperlink with text Click here to forcibly terminate running steps...(first green one) Now I pressed the link...) After the link execution a message about Still paused appeared with another Link Click here to forcibily kill entire build (second green one) After pressing this link also the build finally was hard killed...

So this seems to work without any special plugins (except the multibranch-pipeline build plugin itself).

de-jcup
  • 1,402
  • 12
  • 27
  • If you gave the link that "Click here to forcibly kill the entire build" goes to then I'd up-vote because that would work for me. Unfortunately this solution doesn't because Jenkins fails to show the latest logs because the log file is several GB. – mjaggard Apr 01 '19 at 15:28
  • Sorry, currently I have no longer access to these logs. If I have this failure again, I will add a comment her/update solution. But what about doing a logon at your jenkins machine and just use `tail`or an log viewer to get the link? – de-jcup Apr 02 '19 at 22:38
  • 4
    This worked for me, thanks! @mjaggard: The link is: `Click here to forcibly kill entire build` – kaveish Sep 05 '19 at 13:58
3

VERY SIMPLE SOLUTION

The reason I was seeing this issue was incorrect http link on the page instead of https that should stop the job. All you need to do is to edit onclick attribute in html page, by following

  1. Open up a console log of the job (pipeline) that got hang
  2. Click whatever is available to kill the job (x icon, "Click here to forcibly terminate running steps" etc) to get "Click here to forcibly kill entire build" link visible (it's NOT gonna be clickable at the moment)
  3. Open the browser's console (use any one of three for chrome: F12; ctrl + shift + i; menu->more tools->developer tools)
  4. Locate "Click here to forcibly kill entire build" link manually or using "select an element in the page" button of the console
  5. Double click on onclick attribute to edit its value
  6. Append s to http to have https
  7. Press enter to submit the changes
  8. Click "Click here to forcibly kill entire build" link

Use screenshot for reference enter image description here

Community
  • 1
  • 1
Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
1

I had many zombi-jobs, so I used the following script:

for(int x = 1000; x < 1813; x = x + 1) {
    Jenkins .instance.getItemByFullName("JOBNAME/BRANCH")
    .getBuildByNumber(x)
    .finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"))
}
Stéphane
  • 2,068
  • 22
  • 28
1

Using the Script console at https://my-jenkins/script

import hudson.model.Job
import org.jenkinsci.plugins.workflow.job.WorkflowRun

Collection<Job> jobs = Jenkins.instance.getItem('My-Folder').getAllJobs()
for (int i = 0; i < jobs.size(); i++) {
  def job = jobs[i]
  for (int j = 0; j < job.builds.size(); j++) {
    WorkflowRun build = job.builds[j]
    if (build.isBuilding()) {
      println("Stopping $job ${build.number}")
      build.setResult(Result.FAILURE)
    }
  }
}
Poulad
  • 1,149
  • 1
  • 12
  • 22
1

Three step program

First, abort the build via script console:

Jenkins.instance.getItemByFullName("JobName")
            .getBuildByNumber(JobNumber)
            .finish(
                    hudson.model.Result.ABORTED,
                    new java.io.IOException("Aborting build")
            );

Second, delete the build via script console:

Jenkins.instance.getItemByFullName(jobname).getBuildByNumber(buildnum).delete(); 

Make sure the build is not reachable in the ui.

Third, restart jenkins, for example via the ui: [jenkins_url]/restart

Frankenstein
  • 653
  • 9
  • 18
0

Have had the same problem happen to me twice now, the only fix sofa has been to restart the tomcat server and restart the build.

Ernie
  • 567
  • 2
  • 6
  • 23
0

A utility I wrote called jkillthread can be used to stop any thread in any Java process, so long as you can log in to the machine running the service under the same account.

Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
0

Here is how I fixed this issue in version 2.100 with Blue Ocean

  • The only plugins I have installed are for bitbucket.
  • I only have a single node.

ssh into my Jenkins box
cd ~/.jenkins (where I keep jenkins)
cd job/<job_name>/branches/<problem_branch_name>/builds
rm -rf <build_number>

After this, you can optionally change the number in nextBuildNumber (I did this)

Finally, I restarted jenkins (brew services restart jenkins) This step will obviously be different depending how you manage and install Jenkins.

Tom Bates
  • 586
  • 1
  • 6
  • 14
0

None of these solutions worked for me. I had to reboot the machine the server was installed on. The unkillable job is now gone.

0

If the "X" button is not working and the job is stuck, then just delete the specific build number. It will free up the executor.

In my case, even though the job was completed, it was still stuck in the executor for hours. Deleting the build worked for me.

Nikhil Singh
  • 21
  • 1
  • 3
0

Go to Manage Jenkins then Script Console

Jenkins.instance.getItemByFullName(FULL_JOB_NAME)
    .getBuildByNumber(BUILD_NUMBER)
    .delete()

You can find FULL_JOB_NAME on your job page, right after Full project name: text

PS: .finish() method isn't recognized.

Takman
  • 1,028
  • 10
  • 13
-1

You can just copy the job and delete the old one. If it doesn't matter that you lost the old build logs.

-4

Enter the blue-ocean UI. Try to stop the job from there.

user3360767
  • 868
  • 7
  • 18