Springboot shutdown hook not working with Windows TaskKill
Asked Answered
E

1

2

I have an Apache Camel Spring Boot Java 8 app that runs on Windows 10. It shuts down gracefully when CTRL-C is pressed, although sometimes I have to press it twice. But when using TaskKill, the answer always is

C:\Windows\system32>taskkill /PID 1048
ERROR: The process with PID 1048 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

My goal is to create a Windows service, but again, when stopping the service, the app is killed abruptly. Why CTRL-C is working, and TaskKill not? What can I do to have a Windows service that shuts down gracefully? The main class of my app looks like

import org.apache.camel.spring.Main;

@SpringBootApplication
@EnableScheduling
public class InformerApplication {

  public static void main(String[] args) throws Exception {
    SpringApplication.run(InformerApplication.class, new String[0]);
    Main.main( args);
  }
}
Ebonee answered 24/1, 2020 at 12:50 Comment(3)
There is a difference between "service shutdown" and "process kill" When you killing a process, windows will stop that immediately and the process have not more time to do some cleanups. an shutodwn will give the process some time to return graceful. CTRL+C is an termination Interrupt, no kill command.Selfopinionated
Thanks @Matthias, I do not think that is true. You see, TaskKill only kills this process if /F is present. That is why /F is there.Ebonee
TaskKill (without /F) sending WM_CLOSE to process and CTRL+C is sending SIGINT or SIGBREAKSelfopinionated
S
1

From Java Shutdown Hooks API

The Java virtual machine shuts down in response to two kinds of events:

  • The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked
  • The virtual machine is terminated in response to a user interrupt, such as typing CTRL+C (SIGINT), or a system-wide event, such as user logoff or system shutdown.

https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)

WM_CLOSE Signal from KillTask is not handled.

Selfopinionated answered 24/1, 2020 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.