如何检测从Linux应用程序关机/重启 [英] How do I detect shutdown/reboot from linux app

查看:365
本文介绍了如何检测从Linux应用程序关机/重启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C语言编写的应用程序,它作为后台进程运行,需要通过RS232送东西时,系统处于关机或重新启动状态,它需要这两者之间的区别。
所以我的想法是:
在我的剧本停的情况下我的应用程序的脚本/etc/init.d/my_app,我会运行/ sbin /运行级别的命令来获得当前运行级别:
0 - 关机状态下
6 - 重新启动状态
然后,我将执行一些命令通知我的守护进程的状态吧,守护进程将通过RS进行通信,然后将退出。

I have an application written in C which runs as a daemon and needs to send something through RS232 when system is in shutdown or reboot state, and it needs to distinguish between these two. So my idea is: In my application script /etc/init.d/my_app in "stop" case of my script, I will run /sbin/runlevel command to get current runlevel: 0 - shutdown state 6 - reboot state then I will execute some command to inform my daemon which state is it, daemon will perform communication through rs, and then will exit.

我觉得它应该工作,但它未必是最好的解决方案,尤其是因为我的应用程序已经在运行作为一个守护进程,也许我可以从系统/内核/库或通过Unix套接字或东西直接接收一些信号。

I think it should work, however it may not be the best solution, especially because my app is already running as a daemon, maybe I can receive some signal directly from system/kernel/library or through unix socket or something.

最好的问候

马立克

推荐答案

我不知道该信号被发送到系统关闭的应用程序。我最好的猜测是SIGTERM,如果应用程序没有关闭SIGKILL。那你有没有试图抓住SIGTERM和正确地关闭程序?有很多的范例在网络上如何做到这一点。

I am not sure which signal is send to an application on system shutdown. My best guess is SIGTERM and if the application does not shutdown SIGKILL. So did you try to catch SIGTERM and properly shut down your program? There are a lot of examples on the net how to do that.

对于更复杂的处理过程中,您可以发送SIGUSR1,SIGUSR2到您的应用程序。

For more sophisticated process handling you can send SIGUSR1, SIGUSR2 to your application.

这篇关于如何检测从Linux应用程序关机/重启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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