I need to create a code that would be flexible enough to perform some required cleanups even if the terminal where it was running is closed or SSH connection is lost. I have all the necessary code wrapped into one class’ __del__ method. However, Python doesn’t seem to be calling it (unlike C++) on exit. I tried [atexit][1] but it wasn’t called either.
Any suggestions will be appreciated.
You need to use the
signalmodule.SIGHUPshould be sent to your program on either closing the terminal window or losing the SSH connection. You could catchSIGINTinstead ofSIGHUPbut this is a catch-all solution.You can test this by issuing
sudo kill -1 <pid of your python process>in another terminal window.