简体   繁体   中英

How to get alert for shutdown of python process/abrupt termination?

我们如何在python进程内连接代码,以便在进程关闭/突然终止的情况下发送警报?

It's not clear what exactly you mean. Shutdown/abort of the process itself? Or of a child process?

  • Shutdown/abort of a process itself: Have a look at Pythons atexit module ; here you can register a callback for when your program cleanly exits. But there is absolutely no way for you to catch all circumstances, if your program fails b/oa serious issue (eg segfault) your atexit handlers will never get called. You need a supervising process to catch absolutely all aborts.
  • Shutdown/abort of a child process: If you eg use the subprocess module you can simply call poll() or wait() on popen objects to see if the spawned process is dead / wait for them to die. For a more advanced implementation use Pythons signal module to set a handler for SIGCHLD - this signal is sent to your process whenever one of the child processes terminates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM