How to determine if an instance of your program is already running?

David M. Wilson dw-google.com at botanicus.net
Sat Dec 27 06:42:15 EST 2003


Mike <Mike at kordik.net> wrote...

> Can I get a list of currently running processes?

There is no portable way of doing it, however if you have a POSIX
environment, you can parse the output of ps:

   def get_process_ids():
      output = []
      ps = os.popen('ps -o pid,command -C python')
      ps.readline()

      for line in ps:
         bits = line.lstrip()[:-1].split(' ')
         output.append( (int(bits[0]), ' '.join(bits[1:])) )

      return output


Don't hold me to this - I tested it on BSD and Linux, and as far as I
know both the options I have used are portable.


David.




More information about the Python-list mailing list