I am running a SpringBoot Application. I have added some cleanup action using @PreDestroy annotation. When I terminate the process using SIGTERM that is kill ${PID} of tomcat, it performs all the cleanup tasks by calling method marked with @PreDestroy. However, When I use SIGKILL i.e kill -9 ${PID} of tomcat , the clean up is not performed.
Is there any way to make the application perform all the pre destruction work when it is terminated using SIGKILL?
No, there is not. Regular
kill
waits for the process to exit whilekill -9
does not and kills it immediately. What is important is that the signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored. man 7 signal shows a short summary of what each signal means: