I'm trying to check on the status of the pigpiod process from within a python script. These two methods both appear to work so far. Are there any significant advantages or disadvantages of one over the other? Are these likely to be reliable ways to do this?

import os, commands

def test_1():
        n = int(os.popen('pidof pigpiod').read().splitlines()[0])
        n = None
    return n

def test_2():
        n = int(commands.getstatusoutput('pidof pigpiod')[1])
        n = None
    return n

print "test 1: ", test_1()
print "test 2: ", test_2()

pigpiod is running:

test 1:  2038
test 2:  2038

pigpiod has been stopped:

test 1:  None
test 2:  None

1 Answer 1


Obviously both work, but I would however:

  • avoid to use the os module, but use subprocess instead; specifically subprocess.run(cmd, check=True, shell=True),
  • not use commands at all, as it is deprecated since Python2.6 and removed from Python3,
  • finally: consider not to check that at all with a shell command and go for pigpio's own features. After importing import pigpio you are required to create at least one instance of the pigpio.pi class - and it will tell you if it fails to connect to the pigpiod daemon:

    pi = pigpio.pi()
    if not pi.connected:
        exit() # or reporting the issue

There are some other issues with your code. The except clause is catching all exceptions which is something you probably don't want to do without good reason. As currently written it furthermore returns None if the os or commands call itself fails, say if i.e. pidof is not available (I have no idea if that assumption is reasonable); your test would therefore report a false negative with pigpiod actually running.

  • my goodness it looks like I've hit the jackpot; I will fire up my Pi and give this a test run tomorrow. Thanks!
    – uhoh
    Oct 16, 2018 at 12:20
  • 1
    @uhoh, sorry it took so long to provide an answer...
    – Ghanima
    Oct 16, 2018 at 12:42
  • It's still quite relevant; I'm doing a lot of things at the same time so I can move this to the top of the list no problem.
    – uhoh
    Oct 16, 2018 at 12:50
  • I'm finally back into this bit of my script. There are warnings about using shell=True docs.python.org/2/library/…. I'll try to read now about workarounds. I'm using 2.7 and my subprocess has no .run() but it does have a .call() which does not recognize the check argument, but there is a .check_call() for that perhaps? Those two do work, thanks!
    – uhoh
    Dec 12, 2018 at 2:05
  • Adding this as a comment for anyone else checking out this question: there's no reason not to switch to Python 3 to get subprocess.run(), by adding #!/usr/bin/env python3 to the top of your script if you're running as ./script.py, or by running python3 script.py.
    – lights0123
    Apr 16, 2020 at 1:52

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.