Question

I have a Java application running on a linux server. When it started, it recorded the process id to a "pidfile".

When I close it, I simply read the pid from that file, and kill it:

kill -9 12345

But other people says it is not safe, instead, they suggest me to embed a small http server, and provide a http api for closing, such as:

http://localhost:8080/close

When I want to close it, I need to visit it:

curl http://localhost:8080/close

It works but very boring, I still prefer the "process id" way. Is there any way to use process id to close the application safely?


Update:

"Safely" here I mean I can cancel some running tasks which may writing files to disk and close some resources before exiting.

Was it helpful?

Solution

You can shut it down by executing kill -HUP processId

If there is a need to gracefully finalize work, register your shutdown hook and perform all required finalization there.

More info here Runtime.addShutdownHook

P.S: In my opinion, do not embed web server just for this purpose if you do not really need it. And if you use web server, you need also think about security - anyone who knows the url, can shut your app down remotely:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top