Checking for duplicate instances of a script...

Discussion in 'Python' started by Guillaume Dargaud, Jan 21, 2004.

  1. Hello, python beginner here,
    How can I make sure that a given script is never run more than once at the
    same time in a system ?
    Besides checking 'ps' for its own name. Most modern langages have a direct
    way to do that.
    Thanks
    --
    Guillaume Dargaud
    http://www.gdargaud.net/
    "Here is your parachute and here is the manual.
    Welcome to Linux."
     
    Guillaume Dargaud, Jan 21, 2004
    #1
    1. Advertising

  2. Guillaume Dargaud

    Joakim Hove Guest

    "Guillaume Dargaud" <> writes:

    > Hello, python beginner here,
    > How can I make sure that a given script is never run more than once at the
    > same time in a system ?


    This can probably be done in *many* different ways, maybe there is
    even an agreed upon on best solution. This is a small suggestion
    based on fcntl.flock() (Unix only I am afraid):

    #!/usr/bin/python
    import struct, fcntl
    import sys
    lockfile = "/tmp/.lock"

    ## Open a file-descriptor for the lock file.
    lockH = open(lockfile,"w")

    ## Try to aquire an exclusive lock on the lock file.
    try:
    fcntl.flock(lockH , fcntl.LOCK_EX | fcntl.LOCK_NB)
    except IOError,e:
    sys.exit("Lockfile %s already locked - an instance is probably already running" % lockfile)


    ##
    ## Your code ...
    ##


    ## Release the lock again
    fcntl.flock(lockH, fcntl.LOCK_UN)


    --
    /--------------------------------------------------------------------\
    / Joakim Hove / / (55 5) 84076 | \
    | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
    | CMU | 5231 Paradis |
    \ Thormøhlensgt.55, 5020 Bergen. | 55 91 28 18 /
    \--------------------------------------------------------------------/
     
    Joakim Hove, Jan 21, 2004
    #2
    1. Advertising

  3. > This can probably be done in *many* different ways, maybe there is
    > even an agreed upon on best solution. This is a small suggestion
    > based on fcntl.flock() (Unix only I am afraid):


    Wrong prediction: it works fine in Cygwin under windows, but it doesn't on
    my linux server...

    Traceback (innermost last):
    File "./CounterTest.py", line 24, in ?
    fcntl.flock(lockH, fcntl.LOCK_EX | fcntl.LOCK_NB)
    TypeError: illegal argument type for built-in operation


    Cygwin:
    2.3.3 (#1, Dec 30 2003, 08:29:25)
    [GCC 3.3.1 (cygming special)]

    Linux:
    1.5.2 (#1, Jan 31 2003, 10:58:35) [GCC 2.96 20000731 (Red Hat Linux 7.3 2

    Is this too old ?

    Maybe I'll try with os.open flags...
    --
    Guillaume Dargaud
    http://www.gdargaud.net/
    "My goal is to be a meteorologist. But since I possess no training in
    meteorology, I suppose I should try stock brokerage." - Found in a
    resume.
     
    Guillaume Dargaud, Jan 21, 2004
    #3
  4. Guillaume Dargaud

    William Park Guest

    Guillaume Dargaud <> wrote:
    > Hello, python beginner here,
    > How can I make sure that a given script is never run more than once at the
    > same time in a system ?
    > Besides checking 'ps' for its own name. Most modern langages have a direct
    > way to do that.


    1. Check 'ps'. But, if 2 processes checks at the same, then they see
    nothing is running and decides to run themselves.

    2. Use lockfile.

    --
    William Park, Open Geometry Consulting, <>
    Linux solution for data management and processing.
     
    William Park, Jan 21, 2004
    #4
  5. On Wed, 21 Jan 2004 10:28:20 +0100, "Guillaume Dargaud"
    <> wrote:

    >Hello, python beginner here,
    >How can I make sure that a given script is never run more than once at the
    >same time in a system ?
    >Besides checking 'ps' for its own name. Most modern langages have a direct
    >way to do that.
    >Thanks

    What modern language has a direct way to do that?
    --dang
     
    Dang Griffith, Jan 22, 2004
    #5
  6. > >Hello, python beginner here,
    > >How can I make sure that a given script is never run more than once at the
    > >same time in a system ?
    > >Besides checking 'ps' for its own name. Most modern langages have a direct
    > >way to do that.
    > >Thanks

    > What modern language has a direct way to do that?


    You can register a COM or CORBA interface (is this what they are called?)
    and attempt to connect to it during startup. If you can connect to
    yourself, don't start up, if you can't, start up.

    - Josiah
     
    Josiah Carlson, Jan 22, 2004
    #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. John Wohlbier
    Replies:
    2
    Views:
    398
    Josiah Carlson
    Feb 22, 2004
  2. Replies:
    8
    Views:
    488
    James Stroud
    Jan 29, 2009
  3. William Morris
    Replies:
    1
    Views:
    126
    jenny mabe
    Feb 26, 2004
  4. Vijay Ramane
    Replies:
    2
    Views:
    211
    Vijay Ra
    Dec 30, 2010
  5. Rene Scheibe

    Checking number of instances running

    Rene Scheibe, Aug 7, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    101
    Eric Schwartz
    Aug 7, 2003
Loading...

Share This Page