Why kill -9 Can Crash Your Service and How to Shut Down Spring Boot Gracefully

This article explains the dangers of using the Linux kill -9 command for terminating processes, illustrates how abrupt termination can cause data inconsistency in transactional systems, and provides step‑by‑step methods—including Spring Boot actuator, custom Tomcat shutdown, and @PreDestroy hooks—to achieve graceful service shutdown.

Open Source Linux
Open Source Linux
Open Source Linux
Why kill -9 Can Crash Your Service and How to Shut Down Spring Boot Gracefully

What is kill -9 pid?

kill sends a signal to a process; the default SIGTERM (15) terminates it, while SIGKILL (9) forces immediate termination, effectively killing a Linux process.

Problems caused by kill -9

Using kill -9 abruptly stops services, which can lead to data inconsistency, especially in transactional operations such as money transfers. In MyISAM tables or distributed systems, partial updates may occur, causing loss of consistency and potential financial errors.

Graceful service termination

Instead of kill -9, stop accepting new requests, wait for active threads to finish, then stop the container. The four steps are: stop receiving requests, check running threads, wait for them to finish, and finally stop the server.

Using kill -15 in a Spring Boot test

Example controller that sleeps for 100 seconds. When kill -15 is sent, the thread receives an interrupt, logs show InterruptedException, but the final log still appears.

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

Running the test and sending kill -15 <pid> demonstrates that the end log is printed and the thread is interrupted.

Spring Boot actuator shutdown

Adding spring-boot-starter-actuator and enabling the shutdown endpoint allows graceful shutdown via HTTP.

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

Calling /actuator/shutdown returns a friendly message and stops the application after pending requests finish.

Custom elegant shutdown configuration

Define ElegantShutdownConfig implementing TomcatConnectorCustomizer and ApplicationListener<ContextClosedEvent> to pause the connector and wait for thread‑pool termination (default 10 seconds).

public class ElegantShutdownConfig implements TomcatConnectorCustomizer,
        ApplicationListener<ContextClosedEvent> {
    private volatile Connector connector;
    private final int waitTime = 10;
    @Override
    public void customize(Connector connector) { this.connector = connector; }
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        connector.pause();
        Executor executor = connector.getProtocolHandler().getExecutor();
        if (executor instanceof ThreadPoolExecutor) {
            ThreadPoolExecutor pool = (ThreadPoolExecutor) executor;
            pool.shutdown();
            try {
                if (!pool.awaitTermination(waitTime, TimeUnit.SECONDS)) {
                    System.out.println("Force shutdown");
                }
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

Register this bean in the Spring Boot application and add it to the Tomcat factory.

@Bean
public ElegantShutdownConfig elegantShutdownConfig() {
    return new ElegantShutdownConfig();
}

@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addConnectorCustomizers(elegantShutdownConfig());
    return tomcat;
}

Data backup before shutdown

Annotate a method with @PreDestroy to run backup logic when the container stops.

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

Images illustrate the startup, process ID lookup, and log output during graceful shutdown.

Spring Boot start
Spring Boot start
Spring Boot log after graceful shutdown
Spring Boot log after graceful shutdown
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.

spring-bootGraceful ShutdownLinux kill
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.