How do I detect shutdown/reboot from linux app [duplicate]

試著忘記壹切 提交于 2019-12-22 13:59:29

问题


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.

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.

Best regards

Marek


回答1:


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.

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



来源:https://stackoverflow.com/questions/19269825/how-do-i-detect-shutdown-reboot-from-linux-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!