Keywords: Jenkins | Zombie Jobs | Script Console | Build Termination | Thread Interruption
Abstract: This technical paper provides an in-depth analysis of zombie job issues in Jenkins and presents effective solutions that do not require server restart. When Jenkins jobs run indefinitely without actual execution, traditional interruption methods often fail. By examining Jenkins' internal mechanisms, the paper offers three robust approaches: using the Script Console to directly terminate jobs, interrupting hanging execution threads, and leveraging HTTP endpoints for forced build stoppage. Each method includes detailed code examples and step-by-step instructions, enabling system administrators to resolve zombie job issues efficiently. The paper also discusses practical case studies and important considerations for implementation.
Problem Background and Symptom Analysis
In continuous integration environments, Jenkins, as a core automation tool, occasionally encounters jobs that run for extended periods while actually being in a stalled state. These so-called "zombie jobs" not only consume system resources but may also block subsequent build tasks. User reports indicate that such jobs typically exhibit: no updates in console output, unresponsive stop buttons, and no actual running processes on build servers.
Root Cause Investigation
Zombie jobs typically arise from several factors: plugin compatibility issues, unreleased resource locks, thread deadlocks, or external dependency service failures. As referenced in supplementary materials, certain specific plugins (such as the Katalon plugin) may fail to properly terminate jobs upon execution failure, leading to perpetual running states. In such cases, traditional interface operations often prove ineffective.
Core Solution Approaches
Method 1: Direct Termination via Script Console
This is the most direct and effective solution. Through Jenkins' Script Console, internal APIs can be accessed to directly modify job status:
Jenkins.instance.getItemByFullName("JobName")
.getBuildByNumber(JobNumber)
.finish(hudson.model.Result.ABORTED,
new java.io.IOException("Aborting build")
);
This Groovy code retrieves the build instance of the specified job and invokes the finish method to set its status to ABORTED. Replace JobName with the actual job name and JobNumber with the specific build number.
Method 2: Thread Interruption Technique
For more complex scenarios, particularly those involving thread deadlocks, interrupting specific threads can resolve the issue:
Thread.getAllStackTraces().keySet().each() {
t -> if (t.getName()=="YOUR THREAD NAME" ) { t.interrupt(); }
}
Note that in newer Jenkins versions, the thread interruption method may no longer be applicable, so Method 1 should be prioritized.
Method 3: HTTP Endpoint Forced Stoppage
For Pipeline-type jobs, builds can be stopped by sending HTTP POST requests to specific endpoints:
<BUILD ID URL>/stop- Gracefully aborts a Pipeline<BUILD ID URL>/term- Forcibly terminates a build (use when stop fails)<BUILD ID URL>/kill- Hard kills a pipeline (last resort)
Detailed Implementation Steps
Preparation Phase
Before performing any operations, verify the following information: the job's full name, build number, and job type (Freestyle or Pipeline). Ensure you have Jenkins administrator privileges.
Operational Procedure
- Log into the Jenkins management interface and navigate to "Manage Jenkins" → "Script Console"
- Select appropriate solution code based on the job situation
- Modify job name and build number parameters in the code
- Execute the script and monitor job status changes
- Verify successful job termination
Best Practices and Important Considerations
Preventive Measures
To minimize zombie job occurrences: regularly update Jenkins and plugins, configure appropriate timeout settings, monitor external dependency services, and implement comprehensive error handling mechanisms.
Risk Management
When using forced termination methods, be aware of potential risks: resources may not be properly released, subsequent build stability could be affected, and inconsistent states may occur in distributed environments. Testing in non-production environments is recommended.
Case Analysis and Experience Summary
The Katalon plugin issue mentioned in reference materials serves as a typical example. When using specific testing frameworks, if the framework itself has defects or integrates poorly with Jenkins, zombie jobs are more likely to occur. In such cases, beyond applying the aforementioned solutions, consider: changing test execution methods, using TestSuiteCollection instead of individual Test Suites, and configuring appropriate timeout parameters.
Technical Deep Dive
From a technical perspective, Jenkins job lifecycle management involves multiple components: Executors, Workspaces, Build Queues, etc. When jobs enter zombie states, it typically indicates synchronization issues between these components. The APIs provided by the Script Console bypass normal lifecycle management flows, allowing direct modification of internal states, which is crucial for resolving such problems.
In practical operations, follow this priority order for solution selection: first attempt HTTP endpoint methods (for Pipeline jobs), then use Script Console direct termination, and finally consider thread interruption. Each method has specific application scenarios and limitations that should be evaluated based on the particular situation.