How to know Debian is about to reboot ?


7 posts
by Narf03 » Fri Jul 06, 2012 6:23 am
Hi all.

When I issue a 'sudo reboot' command to pi, pi will have few seconds before it start to kill all running processes and then reboot. I want to know, in Python, are there any method we can use to check the system is shutting down or not ?

Thanks.
Posts: 230
Joined: Mon Jun 11, 2012 3:44 pm
by W. H. Heydt » Fri Jul 06, 2012 6:26 pm
(I don't know Python, but I've done a lot of programming in other languages over the last 50 years...)

Since one of things done during shutdown is to kill all processes, can Python intercept the kill signal?
Posts: 1378
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)
by Narf03 » Sat Jul 07, 2012 8:17 am
oh, a master. hi ya =)

err i donno about that too, im new to both linux and python, but when its shutting down, the pi do broadcast some sort of shut down notice, donno how to intercept this as well. i think intercept the kill signal maybe too late to do anything.
Posts: 230
Joined: Mon Jun 11, 2012 3:44 pm
by W. H. Heydt » Sat Jul 07, 2012 4:41 pm
Narf03 wrote:oh, a master. hi ya =)

err i donno about that too, im new to both linux and python, but when its shutting down, the pi do broadcast some sort of shut down notice, donno how to intercept this as well. i think intercept the kill signal maybe too late to do anything.


I am not an expert on unix/Linux (they're pretty similar, both working the POSIX spec these days), but I beleive that the first thing that will happen is that the system will send a 'kill' signal to a process, basically asking it to shut down nicely. If that fails to do the trick, the system will then do a "sure kill" aka "kill -9" to terminate the process immediately.

*If* a Python program can have error handling routines that deal with signals from the system, *then* it should be able to react to the initial kill signal and shut itself down before the system decides to do the sure kill.

(You could actually test this by, while running your program, issuing a kill to the process ID of your program. You could also try a kill -9 to see the difference. But check the manual page (use "man kill" to see it) to make sure that's still the sure kill flag.)
Posts: 1378
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)
by Joe Schmoe » Sat Jul 07, 2012 5:20 pm
I'm pretty sure that there is a script somewhere (in /etc/init.d and friends) that actually controls what the system does when it shuts down. You could probably mess with that (once you find it...) and get it to do whatever it is you really want to do.

My concern is that the time period between when the process gets the TERM signal and when it gets the KILL signal might (in the default configuration) be too short to be meaningful.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2516
Joined: Sun Jan 15, 2012 1:11 pm
by timothy3592 » Sat Jul 07, 2012 7:42 pm
Programs can't "catch" a SIGKILL (aka kill -9) they can generally catch SIGTERM (-15, and the default "kill" signal). The difference here is that SIGKILL tells the kernel to drop the process without telling it, SIGTERM asks the process to terminate itself. As for catching signals in python, I wouldn't know, I'm not much of a python guru.
Posts: 64
Joined: Wed Jun 13, 2012 6:06 am
by AndrewS » Thu Jul 12, 2012 12:54 pm
Joe Schmoe wrote:I'm pretty sure that there is a script somewhere (in /etc/init.d and friends) that actually controls what the system does when it shuts down. You could probably mess with that (once you find it...) and get it to do whatever it is you really want to do.

https://duckduckgo.com/?q=rc.d+shutdown+scripts has some useful links.
User avatar
Posts: 3091
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK