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

Discussion in 'Python' started by Mike, Dec 27, 2003.

  1. Mike

    Mike Guest

    Does python support named semaphores for Linux? I saw that ActivePython
    for Win32 does.

    Can I get a list of currently running processes?

    I already posted in the GTK forum about looking for the window title.

    Thx
     
    Mike, Dec 27, 2003
    #1
    1. Advertising

  2. Mike <> 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.
     
    David M. Wilson, Dec 27, 2003
    #2
    1. Advertising

  3. Mike

    Mike Guest

    On Sat, 27 Dec 2003 03:42:15 -0800, David M. Wilson wrote:

    > Mike <> 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.

    Excellant. Thank you!
     
    Mike, Dec 28, 2003
    #3
  4. Mike

    Miki Tebeka Guest

    Hello Mike,

    You can have your program create a known directory (which IIRC an
    atomic operation). When a new instance tries to create it, there will
    be an exception.

    Make sure that when you application is closing to remove this
    directory.

    HTH.
    Miki
     
    Miki Tebeka, Dec 28, 2003
    #4
  5. Mike

    Jarek Zgoda Guest

    Miki Tebeka <> pisze:

    > You can have your program create a known directory (which IIRC an
    > atomic operation). When a new instance tries to create it, there will
    > be an exception.
    >
    > Make sure that when you application is closing to remove this
    > directory.


    Traditional way is to create pidfile somewhere -- in user's home
    directory, /var/run...

    --
    Jarek Zgoda
    Unregistered Linux User #-1
    http://www.zgoda.biz/ JID:zgoda-a-chrome.pl http://zgoda.jogger.pl/
     
    Jarek Zgoda, Dec 28, 2003
    #5
  6. Mike

    Ben Finney Guest

    On Sun, 28 Dec 2003 08:00:33 +0000 (UTC), Jarek Zgoda wrote:
    > Traditional way [to avoid multiple instances] is to create pidfile
    > somewhere -- in user's home directory, /var/run...


    More precisely, the "pidfile" is a file named "processname.pid", which
    contains a single line of text: the process ID (number) of the running
    process. /var/run/processname.pid is the usual place. When the program
    is requested to exit, it deletes the file while cleaning up.

    On startup the program (or some simple wrapper script) checks the
    existence of this file; if it exists, it reads the PID contained in the
    file and checks the status of the process. It then quits immediately if
    the process is already running.

    What to do in the case that the PID file exists, but the process isn't
    running, is up to you to decide; possibly you can warn the user that a
    prior instance of the application didn't exit properly.

    --
    \ "Anytime I see something screech across a room and latch onto |
    `\ someone's neck, and the guy screams and tries to get it off, I |
    _o__) have to laugh, because what is that thing?" -- Jack Handey |
    Ben Finney <http://bignose.squidly.org/>
     
    Ben Finney, Dec 28, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Sinclair

    How to check for already running program?

    Nick Sinclair, Jul 1, 2005, in forum: C Programming
    Replies:
    4
    Views:
    448
    Nick Sinclair
    Jul 4, 2005
  2. Strato
    Replies:
    6
    Views:
    341
  3. Steven Kauffmann
    Replies:
    2
    Views:
    377
    Francesco Bochicchio
    Jul 27, 2011
  4. Suraj N. Kurapati

    using rcov in already running program

    Suraj N. Kurapati, Sep 4, 2006, in forum: Ruby
    Replies:
    2
    Views:
    105
    Mauricio Fernandez
    Sep 10, 2006
  5. Olive
    Replies:
    4
    Views:
    251
    Olive
    Jul 6, 2012
Loading...

Share This Page