Hi all. Another newbe question. I have a flow which accepts input from Alexa and interacts with a Python program via mqtt. I only want to launch the program if it's not running. Seems like this should be simple but I can't figure out the logic. I can easily set up mqtt topics and commands to report that the program is running but there are no messages to listen for if the program is not running. Any help would be appreciated.
Is the python program your own? If so then the easiest solution might be to modify it slightly to detect whether it is already running when it starts, and just exit immediately if so.
Alternatively then you could run something like
ps -aux from an exec node and interpret the result to see if it is running.
The following code should do the job, it is cross-platform and runs on Python 2.4-3.2. I tested it on Windows, OS X and Linux.
from tendo import singleton
me = singleton.SingleInstance() # will sys.exit(-1) if other instance is running
The latest code version is available singleton.py. Please file bugs here.
You can install tend using one of the following methods:
pip install tendo
manually by getting it from tendo · PyPI
Brilliant guys. Thanks for the quick response.