I have a python3 script which is started by systemd automatically on boot. This script turns on a green LED to signal that the script has started.

Now I want the LED to turn off if the script fails/crashes. How would I go about doing this? I know how to turn off the LED in my code, but if the script crashes, nothing more executes to turn off my LED.

Do I need a secondary script with its own systemd service which monitors my main script and turns off the LED when the primary script crashes, or is there any better/cleaner solution to this problem?

  • 1
    What could cause your script to crash? Are you using try except to catch anything that could be an issue?
    – CoderMike
    Mar 4 at 9:49
  • Well, like CoderMike said, you can just try: and except (your exception that gets raised):and then in the except part just turn the LED off. Mar 4 at 12:44
  • sounds like you need a watchdog program to monitor the health of your main script ... have a shared data that the main script can update and the watchdog check for periodic updates
    – jsotola
    Mar 4 at 17:25

You can use a wrapper script. When the python script crashes, the wrapper script can turn off the LED.

So, the wrapper script would be a simple as:

echo 0 >/sys/class/leds/led0/brightness

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.