I have a Python-script which is started at bootup. In this I am trying to detect when the Raspberry is shutting down and at that time write some data to a file. However, the SIGTERM-handler is not getting activated.
What I have is this
import signal
import time
stopped = False
out = open('log.txt', 'w')
def stop(sig, frame):
global stopped
stopped = True
out.write('caught SIGTERM\n')
out.flush()
signal.signal(signal.SIGTERM, stop)
while not stopped:
out.write('running\n')
out.flush()
print(str(time.time())+ "," + str(stopped))
time.sleep(1)
print("Caught Shutdown")
I am testing this using two Putty-terminals. In my first terminal I have the script executed, in the other one I am putting in
sudo shutdown -r
What I get in the first window with the Python-output is this:
1645909452.4044187,False
1645909453.4057834,False
Broadcast message from root@mypi on pts/1 (Sat 2022-02-26 22:04:14 CET):
The system is going down for reboot at Sat 2022-02-26 22:05:14 CET!
1645909454.4071815,False
1645909455.4085443,False
Any idea why the SIGTERM is not "accepted"? Thanks a lot already for ideas or pointers