简体   繁体   中英

Is there any way to close a Java application by process id?

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.

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

PS: 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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM