'Fire custom function before kill, cancel, close terminal

How can I fire function before close/kill/cancel process. I need to fire and pause function and after that exit from console. Program in python, always I click ctrl + c but it not working and kill process from console already do not work. I need it for all possible actions by terminal.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source