Quick revision
The short answer
The pod was killed before Spring Batch could record the end of the run, so the job execution, and the step execution that was running, still say STARTED, and Spring Batch refuses to launch the instance again because it believes it is running. After confirming no process is really running it, we mark the stale execution and its step as FAILED with an end time, using the JobOperator support newer versions provide or a careful SQL update, and then restart. To prevent it, the app should handle SIGTERM by calling JobOperator.stop so the job ends STOPPED and restartable, and the platform should detect stale STARTED executions automatically.
What to remember
In this answer, we saw that a pod killed during a node drain never writes its final status, so the job repository shows STARTED forever and Spring Batch refuses new launches with JobExecutionAlreadyRunningException. We recover by confirming nothing is running, marking the execution and its steps FAILED with an end time, through JobOperator support in newer versions or a careful SQL update, and restarting from the last commit. We prevent it by stopping jobs with JobOperator.stop on SIGTERM so they end STOPPED, with a sufficient grace period, and by detecting stale STARTED executions automatically.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The pod was killed before Spring Batch could record the end of the run, so the job execution, and the step execution that was running, still say STARTED, and Spring Batch refuses to launch the instance again because it believes it is running. After confirming no process is really running it, we mark the stale execution and its step as FAILED with an end time, using the JobOperator support newer versions provide or a careful SQL update, and then restart. To prevent it, the app should handle SIGTERM by calling JobOperator.stop so the job ends STOPPED and restartable, and the platform should detect stale STARTED executions automatically.
Why this question is asked
Kubernetes moves pods around all the time: nodes are drained for upgrades, pods are evicted for resources, and spot instances disappear. A web service survives this easily because a new pod takes the next request. A batch job is different, because its state is in the job repository, and a pod that is killed mid-run cannot write the final status. The result is an execution stuck in STARTED that blocks every new launch. Interviewers ask this because it happens in every containerised batch platform sooner or later. In this answer, we will see why the status stays STARTED, why that blocks new launches, how to recover safely, how to shut down gracefully, and how to detect the problem automatically.
1Why the Status Stays STARTED
When a job starts, Spring Batch writes its JobExecution with status STARTED to the job repository, and the running step's StepExecution likewise. At each chunk commit it updates the step's counts and the LAST_UPDATED column. Only when the job ends, normally or with an error, does it write the final status, such as COMPLETED or FAILED, and an end time.
Draining a node sends the pod a SIGTERM and, after the grace period, which is 30 seconds by default, a SIGKILL. If the application does not react to SIGTERM by stopping the job, or the job cannot reach a safe point in time, the JVM is killed in the middle of a chunk. That chunk's transaction is rolled back by the database, but nothing ever writes the final status. The repository therefore still says STARTED, with no end time, indefinitely.
8 messages in order. Play them, or step through them one at a time.
2Why New Launches Fail
Before launching, Spring Batch checks the instance's executions. If one of them has a status that means it is still active, it throws JobExecutionAlreadyRunningException. This rule is important in normal operation: it stops two processes from running the same job instance at the same time and corrupting each other's work. The problem is that Spring Batch cannot tell a running execution from a dead one, because a dead process never says it is dead. So the safety rule now blocks recovery.
3Recovering Safely
Recovery has three steps, and the first one is the most important. Confirm that no process is running the execution: check that the pod is gone, that no other pod picked it up, and that LAST_UPDATED has stopped changing. Marking a live execution as failed and launching another would put two processes on the same data.
Then mark the stale execution as failed. Newer versions of Spring Batch add support in JobOperator for recovering a job execution left in this state, so check your version first. Otherwise, a careful SQL update on the job repository does the same: set the job execution and its running step executions to FAILED, give them an end time, and record why. Finally, restart the instance normally. Because the step's ExecutionContext was saved at its last commit, the restart resumes after the last committed chunk.
Notice that we set FAILED rather than ABANDONED. An abandoned execution cannot be restarted, so ABANDONED is for runs we do not want to continue. FAILED keeps the instance restartable. Any manual change to the repository should follow a runbook and be recorded, because it is the system's record of what happened.
4Shutting Down Gracefully on SIGTERM
The better solution is to avoid the stale status in the first place. When the pod receives SIGTERM, Spring's application context starts closing. We hook into that and ask Spring Batch to stop running executions with JobOperator.stop. Stopping is cooperative: the step finishes its current chunk, commits, and then ends with status STOPPED. A stopped execution is restartable, so the next pod simply restarts it.
Two details make this work. The grace period must be long enough for the slowest chunk to finish, so we set terminationGracePeriodSeconds above the longest expected chunk time, and keep chunks small enough that this is a reasonable number. And the job must actually be running in this pod for the stop to take effect promptly. Recent Spring Batch versions have improved support for graceful shutdown; check your version for what is built in. Even with all this, a SIGKILL or a node crash can still leave a STARTED execution, so we still need detection.
5Detecting Stale Executions Automatically
The batch platform should find these cases before an operator does. A stale execution has a recognisable signature: status STARTED, no end time, and a LAST_UPDATED that is much older than its chunks normally take, together with no live pod for it. A small scheduled check queries the repository and either alerts the owning team or, when the platform can confirm that the pod is gone, marks the execution failed automatically and records it.
- STARTED, no end time, not updated for 30 min?YesCheck the podNoHealthy: do nothing
- Is a pod still running it?YesAlert: possibly hungNoMark FAILED with a reason
- Restart the instanceresumes from the last commit
- Record the actionrunbook and audit log
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why not just delete the stale execution rows?
Deleting them destroys the history and the step's saved ExecutionContext, so the restart would lose its position and might start from the beginning. Marking the execution failed keeps both the record and the restart point.
Could LAST_UPDATED alone tell us the job is dead?
Not on its own. A step with a very slow chunk, or a tasklet that runs a long query, may not update it for a while even though it is healthy. That is why we combine it with checking whether the process exists, and set the threshold well above the normal time between commits.
Does Kubernetes restarting the pod fix this?
No. A restarted pod is a new process that tries to launch the job and meets the same STARTED execution. Recovery needs the status corrected first, either by the stop-on-shutdown path or by the stale detection.
7Conclusion
In this answer, we saw that a pod killed during a node drain never writes its final status, so the job repository shows STARTED forever and Spring Batch refuses new launches with JobExecutionAlreadyRunningException. We recover by confirming nothing is running, marking the execution and its steps FAILED with an end time, through JobOperator support in newer versions or a careful SQL update, and restarting from the last commit. We prevent it by stopping jobs with JobOperator.stop on SIGTERM so they end STOPPED, with a sufficient grace period, and by detecting stale STARTED executions automatically.
Open with
When the node was drained, the pod got SIGTERM and then SIGKILL, and the JVM died in the middle of the job without writing the final status. The job repository still has the job execution and the running step execution as STARTED with no end time, and Spring Batch cannot tell a dead execution from a live one, so every new launch fails with JobExecutionAlreadyRunningException, which normally protects us from two processes running the same instance.
Then, if they want more
To recover, I first confirm that nothing is running it: the pod is gone and LAST_UPDATED has stopped moving. Then I mark the execution and its step as FAILED with an end time and a reason. Newer versions of JobOperator have support for this, so I would check the version; otherwise a careful, recorded SQL update on the batch tables.
I use FAILED, not ABANDONED, because abandoned cannot be restarted. Then I restart the instance and it resumes after the last committed chunk.
To prevent it, the application listens for context shutdown and calls JobOperator.stop on running executions, so the step finishes its chunk and ends STOPPED, which is restartable, and the pod's termination grace period is longer than one chunk.
Finally, the platform runs a check for executions STARTED with an old LAST_UPDATED and no live pod, and alerts or fixes them automatically.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Batch reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After a Kubernetes node was drained mid-run, a Spring Batch job shows status STARTED in...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/0597aee259/