Skip to content
Advertisement

Stop Java service gracefully, executed using nohoup command

I am a beginner in java spring-boot

when I start the java server through

nohup java -jar demo-0.0.1-SNAPSHOT.jar

command my threads start their operations

After sometimes when I stop my server using

kill -9 processid

So my running thread data are lost.

I want to maintain a thread flag in the database whenever server stops.

I have tried to achieve the above scenario in following this tutorial Spring Boot Shutdown. It works properly but it breaks my security as anyone can use this API to stop my server.

I need a way to terminate my server gracefully and call ContextClosedEvent class.

Advertisement

Answer

No graceful shutdown support provided by Spring Boot is going to work if you use kill -9. That is SIGKILL, which can’t be caught, blocked, or ignored, so of course your application can’t do anything in response to it.

Use kill -SIGTERM to allow shutdown hooks to execute in your Java runtime.

Avoid magic numbers like 15 and 9; use meaningful symbols like SIGTERM instead.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement