2

So in essence, I have a python script running as a systemd service. When I tell it to stop, there is a sleep in there that is longer than the systemd timout, and systemd kills it before the last bit of code runs. The code prior to that is a while loop, and the loop repeats every 15 seconds. How can I make it so the script breaks the loop when systemd sends it a signal?

Tyler K
  • 23
  • 3

1 Answers1

0

Register your handler with signal.signal like this:

#!/usr/bin/env python
import signal
import sys

isKilled = False

def signal_handler(signal, frame):
    global isKilled
    isKilled = True

signal.signal(signal.SIGINT, signal_handler)
signal.pause()

You can then test for isKilled to see if the program has been sent a kill request.

Note: You may have to change the value of SIGINT to match the command systemd is sending. It will depend on your use case.

Milk
  • 2,469
  • 5
  • 31
  • 54