The Risks of kill -9 on Spring Boot and How to Shut Down Gracefully
This article explains why using the forceful kill -9 command to terminate Spring Boot services can cause data inconsistency and errors, and demonstrates three safer shutdown methods—using kill -15, invoking ConfigurableApplicationContext.close(), and leveraging Spring Boot Actuator—complete with code examples and configuration steps.
kill can send a specified signal to a program. The default signal is SIGTERM (15), which terminates the program. If the program does not stop, SIGKILL (9) can be used to force termination. Process IDs can be obtained with the ps or jobs commands.
kill -9 pid ???
The command is used to forcibly kill a Linux process. While it works for simple cases, using it in production projects can cause serious problems.
Problems caused by kill -9
Because kill -9 is a violent termination, it can lead to data loss or inconsistency. For example, in a money transfer scenario, if power is cut while debiting account A and crediting account B, using kill -9 is equivalent to an abrupt power loss, potentially leaving A debited and B not credited.
With InnoDB (transactional) storage engine, the transaction would roll back, but with MyISAM (non‑transactional) it would cause permanent data corruption.
In distributed systems, abruptly killing a service can break distributed transactions, leading to unrecoverable loss.
Therefore, kill -9 should not be used to stop services; instead, use graceful shutdown mechanisms.
Graceful shutdown steps
Step 1: Stop accepting new requests and internal threads.
Step 2: Check if any threads are still running.
Step 3: Wait for running threads to finish.
Step 4: Stop the container.
① kill -15 pid
This sends SIGTERM, allowing the JVM to interrupt threads gracefully.
@GetMapping(value = "/test")
public String test(){
log.info("test --- start");
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("test --- end");
return "test";
}Run the Spring Boot application (e.g., sudo mvn spring-boot:run), find its PID, invoke the test endpoint to put the thread to sleep, then execute kill -15 <pid>. The logs show that the thread receives an interrupt, prints the end message, and the process shuts down without a hard crash.
② ConfigurableApplicationContext close
Inject the application context and call close() to trigger a graceful shutdown.
package com.ymy.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class TestController implements ApplicationContextAware {
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
@GetMapping(value = "/test")
public String test(){
log.info("test --- start");
try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); }
log.info("test --- end");
return "test";
}
@PostMapping(value = "shutdown")
public void shutdown(){
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context;
ctx.close();
}
}The close() method removes the JVM shutdown hook and stops the application cleanly.
③ Spring Boot Actuator shutdown endpoint
Add the actuator dependency and enable the /shutdown endpoint.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> server:
port: 9988
management:
endpoints:
web:
exposure:
include: shutdown
endpoint:
shutdown:
enabled: true
server:
port: 8888After starting the application, sending a request to /actuator/shutdown returns a friendly message and gracefully stops the service, similar to the previous methods.
Data backup during shutdown
Use the @PreDestroy annotation to execute backup logic before the container stops.
package com.ymy.config;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PreDestroy;
@Configuration
public class DataBackupConfig {
@PreDestroy
public void backData(){
System.out.println("Backing up data......");
}
}When the application shuts down, the method prints a backup message, demonstrating where to place real backup operations.
Overall, the article shows three reliable ways to stop a Spring Boot service—using kill -15, invoking ConfigurableApplicationContext.close(), and leveraging the Actuator shutdown endpoint—each preserving data integrity and allowing optional cleanup tasks such as data backup.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
