如何检测在Linux上挂起系统关机? [英] How to detect pending system shutdown on Linux?

查看:220
本文介绍了如何检测在Linux上挂起系统关机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,我需要检测系统关闭。
但是,我还没有找到任何可靠的方式获得该事件的通知。

I am working on an application where I need to detect a system shutdown. However, I have not found any reliable way get a notification on this event.

我知道,在关机时,我的应用程序将收到后跟一个 SIGKILL A SIGTERM 信号。我想知道是否有任何的方式,如果要查询 SIGTERM 关机顺序的一部分?

I know that on shutdown, my app will receive a SIGTERM signal followed by a SIGKILL. I want to know if there is any way to query if a SIGTERM is part of a shutdown sequence?

是否有任何人知道,如果有查询的编程(C API)的方式?

Does any one know if there is a way to query that programmatically (C API)?

据我所知,系统不提供任何其他方法来查询即将关机。如果确实如此,这将解决我的问题也是如此。我一直在尝试运行级别为好,但在运行级别的变化似乎是瞬间,没有任何事先警告。

As far as I know, the system does not provide any other method to query for an impending shutdown. If it does, that would solve my problem as well. I have been trying out runlevels as well, but change in runlevels seem to be instantaneous and without any prior warnings.

推荐答案

有没有办法来确定是否 SIGTERM 是关闭过程的一部分。为了检测关机顺序您可以使用使用的rc.d 像ereOn和埃里克Sepanson脚本建议或类似的DBus使用的机制。

There is no way to determine if a SIGTERM is a part of a shutdown sequence. To detect a shutdown sequence you can either use use rc.d scripts like ereOn and Eric Sepanson suggested or use mechanisms like DBus.

不过,从设计的角度来看它是没有意义忽略 SIGTERM ,即使它不是关机的一部分。的 SIGTERM 主要目的是的礼貌地询问的应用程序完全退出,这是不是可能是有人有足够的权限会发出 SIGTERM ,如果他/她不希望应用程序退出。

However, from a design point of view it makes no sense to ignore SIGTERM even if it is not part of a shutdown. SIGTERM's primary purpose is to politely ask apps to exit cleanly and it is not likely that someone with enough privileges will issue a SIGTERM if he/she does not want the app to exit.

这篇关于如何检测在Linux上挂起系统关机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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