I have this python program:
import logging
import time
import signal
import sys
class ClarkeTest:
def __init__(self):
# Configure logging
logging.basicConfig(filename='clarke_test.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Set up signal handler for SIGINT (Ctrl+C)
signal.signal(signal.SIGINT, self.signal_handler)
signal.signal(signal.SIGTERM, self.signal_handler)
def signal_handler(self, sig, frame):
logging.info("Received SIGINT. Exiting...")
sys.exit(0)
def main(self):
while True:
logging.info("Hello Victor")
time.sleep(5)
if __name__ == "__main__":
ClarkeTest().main()
When I execute this program in the console, and press "ctrl+c" a sigint signal is sent to my program, so it logs this message: logging.info("Received SIGINT. Exiting...")
When I converted the python program to .exe with auto_py_to_exe and I installed and started the service with nssm:
- The loggin in the infinite loop works well
- The python program (.exe) does not receive the control-c signal (sigint). Therefore the method signal_handler is not executed.
So what signal is the python program receiving and I can monitor to do cleanup/execute signal handler method when service stops?
Many thanks.