title: Python SIGTERM-handler not executed
body: <p>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.</p>
  <p>What I have is this</p>
  <pre><code>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())+ &quot;,&quot;  + str(stopped))
  
      time.sleep(1)
  
  print(&quot;Caught Shutdown&quot;)
  </code></pre>
  <p>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</p>
  <pre><code>sudo shutdown -r
  </code></pre>
  <p>What I get in the first window with the Python-output is this:</p>
  <pre><code>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
  </code></pre>
  <p>Any idea why the SIGTERM is not &quot;accepted&quot;?
  Thanks a lot already for ideas or pointers</p>