Graceful Shutdown of Spring Boot Services: Why kill -9 Is Dangerous and How to Stop Applications Properly

This article explains the risks of using the Linux kill -9 command to terminate Spring Boot applications, demonstrates how it can cause data inconsistency and thread interruptions, and provides several graceful shutdown techniques—including SIGTERM, Spring's ConfigurableApplicationContext, Actuator endpoints, and custom Tomcat shutdown hooks—to safely stop services and optionally perform data backup.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Graceful Shutdown of Spring Boot Services: Why kill -9 Is Dangerous and How to Stop Applications Properly

kill -9 pid ???

The kill command sends signals to processes; the default is SIGTERM (15). When a process does not stop, SIGKILL (9) can be used, but it forcefully terminates the process and may cause serious issues in production environments.

Problems Caused by kill -9 pid

Using kill -9 is equivalent to a sudden power loss. For example, if a transaction updates two tables and the process is killed, only one table may be updated, leading to data inconsistency, especially with MyISAM tables or distributed services.

In Java, a forced kill triggers an InterruptedException in sleeping threads, which can leave resources unreleased.

Graceful Shutdown Methods

kill -15 pid (SIGTERM)

Sending SIGTERM allows the application to handle shutdown logic. The article shows a simple Spring Boot controller that sleeps for 100 seconds and demonstrates that after sending kill -15 14086, the thread is interrupted and the method logs an exception.

@GetMapping("/test")
public String test() {
    log.info("test --- start");
    try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); }
    log.info("test --- end");
    return "test";
}

Using Spring's ConfigurableApplicationContext

Implement ApplicationContextAware and call ((ConfigurableApplicationContext)context).close(); to stop the application.

public void shutdown() {
    ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context;
    ctx.close();
}

Spring Boot Actuator

Add the spring-boot-starter-actuator dependency and enable the shutdown endpoint in application.yml. A request to /actuator/shutdown returns a friendly message and stops the service.

management:
  endpoints:
    web:
      exposure:
        include: shutdown
  endpoint:
    shutdown:
      enabled: true

Custom Elegant Shutdown with Tomcat

Create a TomcatConnectorCustomizer that pauses the connector and waits for the thread pool to finish before the JVM exits.

public void onApplicationEvent(ContextClosedEvent event) {
    connector.pause();
    Executor executor = connector.getProtocolHandler().getExecutor();
    if (executor instanceof ThreadPoolExecutor) {
        ThreadPoolExecutor pool = (ThreadPoolExecutor) executor;
        pool.shutdown();
        if (!pool.awaitTermination(waitTime, TimeUnit.SECONDS)) {
            System.out.println("Please try forceful shutdown");
        }
    }
}

Data Backup on Shutdown

Annotate a bean method with @PreDestroy to execute backup logic before the container stops.

@PreDestroy
public void backData() {
    System.out.println("Backing up data...");
}

The article concludes that using graceful shutdown techniques avoids data loss, allows proper resource cleanup, and can be extended with custom backup actions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Graceful ShutdownActuatorspring-bootkill -9Thread.interrupt
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.