Search code examples
pythonlinuxflasksubprocesspython-os

How can I check in Python is my Flask app is already running in background


I am just trying to figure out before running my Flask app I need to check is the Flask app is running already running (in the background).

I tried using subprocess library but that doesn't seem to work.

processes = subprocess.Popen('tasklist', stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]

Solution

  • I wonder why you want to know it?

    If you want to restart your app when it crashed, or your server rebooted, you can do that with systemd or with supervisor.

    If you really want to know whether there is a process running, there are many ways...

    For example you can get the current processes like this...

    os.system("ps aux")
    

    Then, it certainly depends on how you started your application.

    I start Flask with python wsgi.py, so I look for that line and the I find...

    jugmac00 12786  3.1  0.1  78680 28068 pts/1    S+   16:46   0:00 python wsgi.py