So im trying to make a Python script that constantly tests if a services is running and output the status via a red LED. I have a script:

    import RPi.GPIO as GPIO
    import time
    import os
    import subprocess

    GPIO.setup(21, GPIO.OUT)

    GPIO.output(21, False)

    while True:

            stream = os.popen('systemctl is-active smbd')
            output = stream.readlines()
            if "in" in output:
                    GPIO.output(21, True)
                    print ("in")
                    GPIO.output(21, False)
                    print ("no")
            print (output)

when im running it though it can read the output of the systemctl check and outputs it via the print(output) but the if "in" in output can't detect the in as in Inactive. But if I set the output String output = inactive It can detect the "In". The print outputs:

['active\n'] or ['inactive\n']

According to the actual status. But if I enter this string into the script i get an error regarding the output. That it has an syntax error.

  • but the outpuut is not Inactive it is inactive
    – Steve
    Aug 3, 2023 at 15:54
  • I re-read your post ... are you saying that you get an error?
    – jsotola
    Aug 3, 2023 at 15:57
  • so if i run the script the it always prints "no" as in the service is detected as active, the error im getting ocurrs when im inputing the printed string from the systemctl cmd into the output string so `output = '['active\n']' gives me a syntax, but the script itself is working fine except it not detecting the change in activity
    – Steve
    Aug 3, 2023 at 16:12

1 Answer 1


output is a list of strings returned from stream.readlines().

A crude way to get the first line would be to use:

if "in" in output[0]:
  • tried it and it works now thx for the help
    – Steve
    Aug 3, 2023 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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