如何优雅地停止java进程? [英] How to stop java process gracefully?

查看:290
本文介绍了如何优雅地停止java进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Linux和Windows中正常停止Java进程?

How do I stop a Java process gracefully in Linux and Windows?

何时 Runtime.getRuntime()。addShutdownHook 被叫,什么时候不被?

When does Runtime.getRuntime().addShutdownHook get called, and when does it not?

终结者怎么样,他们在这里有帮助吗?

What about finalizers, do they help here?

我可以从shell向Java进程发送某种信号吗?

Can I send some sort of signal to a Java process from a shell?

我正在寻找最好的便携式解决方案。

I am looking for preferably portable solutions.

推荐答案

在未强制终止VM的所有情况下执行关闭挂钩。因此,如果您要从kill命令发出标准kill( SIGTERM ),那么它们将执行。类似地,它们将在调用 System.exit(int)之后执行。

Shutdown hooks execute in all cases where the VM is not forcibly killed. So, if you were to issue a "standard" kill (SIGTERM from a kill command) then they will execute. Similarly, they will execute after calling System.exit(int).

然而硬杀( kill -9 kill -SIGKILL )然后它们将不会执行。同样(显然)如果你从计算机上取下电源,将它放入一桶沸腾的熔岩中,或者用大锤将CPU打成碎片,它们就不会执行。不过你可能已经知道了。

However a hard kill (kill -9 or kill -SIGKILL) then they won't execute. Similarly (and obviously) they won't execute if you pull the power from the computer, drop it into a vat of boiling lava, or beat the CPU into pieces with a sledgehammer. You probably already knew that, though.

终结器也应该运行,但最好不要依赖它来进行关机清理,而是依赖你的关机钩子来干净利落地停下来。并且,一如既往,小心死锁(我已经看到太多关闭挂钩挂起整个过程)!

Finalizers really should run as well, but it's best not to rely on that for shutdown cleanup, but rather rely on your shutdown hooks to stop things cleanly. And, as always, be careful with deadlocks (I've seen far too many shutdown hooks hang the entire process)!

这篇关于如何优雅地停止java进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆